--- ftp.c.orig	Mon Oct  4 20:57:29 1999
+++ ftp.c	Mon Oct  4 20:39:33 1999
@@ -473,52 +473,87 @@
 
  /* Create the data connection. */
 
- if(write_string(server_ctrl,"PASV\r\n")==-1)
-   {
-    msg=PrintMessage(Warning,"Failed to write 'PASV' command to remote FTP host [%!s].");
-    return(msg);
-   }
+ if(write_string(server_ctrl,"EPSV\r\n")==-1) {
+     msg=PrintMessage(Warning,"Failed to write 'EPSV' command to remote FTP host [%!s].");
+     return(msg);
+ }
 
  do
-   {
-    str=read_line_or_timeout(server_ctrl,str,SocketTimeout);
-    PrintMessage(ExtraDebug,"FTP: sent 'PASV'; got: %s",str);
-   }
+ {
+     str=read_line_or_timeout(server_ctrl,str,SocketTimeout);
+#if DEBUG_FTP
+     PrintMessage(Debug,"FTP: sent 'EPSV'; got: %s",str);
+#endif
+ }
  while(str && (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || str[3]!=' '));
 
- if(!str || atoi(str)!=227)
-   {
-    if(str)
-      {
-       char *p=str+strlen(str)-1;
-       while(*p=='\n' || *p=='\r') *p--=0;
-       msg=PrintMessage(Warning,"Got '%s' message after sending 'PASV' command",str);
-      }
-    else
-       msg=PrintMessage(Warning,"No reply from FTP server to 'PASV' command; timed out?");
-    return(msg);
-   }
-
- if((host=strchr(str,',')))
-   {
-    while(isdigit(*--host));
-    host++;
-   }
-
- if(!host || sscanf(host,"%*d,%*d,%*d,%*d%n,%d,%d",&l,&port_h,&port_l)!=2)
-   {
-    char *p=str+strlen(str)-1;
-    while(*p=='\n' || *p=='\r') *p--=0;
-    msg=PrintMessage(Warning,"Got '%s' message after sending 'PASV' command, cannot parse.",str);
-    return(msg);
-   }
-
- host[l]=0;
- for(;l>0;l--)
-    if(host[l]==',')
-       host[l]='.';
+ if(!str) {
+     msg=PrintMessage(Warning,"No reply from FTP server to 'EPSV' command; timed out?");
+     return(msg);
+ }
+
+ if(atoi(str) == 229) {
+     host=strchr(str,'(') + 1;
+     if(sscanf(host,"%*c%*c%*c%d%*c",&port_h) != 1) {
+         char *p=str+strlen(str)-1;
+         while(*p=='\n' || *p=='\r') *p--=0;
+         msg=PrintMessage(Warning,"Got '%s' message after sending 'EPSV' command, cannot parse %d.",str, port_h);
+         return(msg);
+     }
+     if (SocketRemoteName(server_ctrl, &host, NULL, NULL)) {
+         msg=PrintMessage(Warning,"Cannot determine server address.");
+         return(msg);
+     }
+ } else if (atoi(str) != 500) {
+     char *p=str+strlen(str)-1;
+
+     while(*p=='\n' || *p=='\r') *p--=0;
+     msg=PrintMessage(Warning,"Got '%s' message after sending 'EPSV' command",str);
+     return(msg);
+ } else { /* Let's try PASV instead then */
+     if(write_string(server_ctrl,"PASV\r\n")==-1) {
+         msg=PrintMessage(Warning,"Failed to write 'PASV' command to remote FTP host [%!s].");
+         return(msg);
+     }  
+ 
+     do
+     {
+         str=read_line_or_timeout(server_ctrl,str,SocketTimeout);
+#if DEBUG_FTP
+         PrintMessage(Debug,"FTP: sent 'PASV'; got: %s",str);
+#endif
+     }
+     while(str && (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || str[3]!=' '));
+ 
+     if(!str) {
+         msg=PrintMessage(Warning,"No reply from FTP server to 'PASV' command; timed out?");
+         return(msg);
+     }  
+ 
+     if(atoi(str) == 227) {
+ 
+         if((host=strchr(str,',')))
+         {
+             while(isdigit(*--host));
+             host++;
+         }
+   
+         if(!host || sscanf(host,"%*d,%*d,%*d,%*d%n,%d,%d",&l,&port_h,&port_l)!=2)
+         {
+             char *p=str+strlen(str)-1;
+             while(*p=='\n' || *p=='\r') *p--=0;
+             msg=PrintMessage(Warning,"Got '%s' message after sending 'PASV' command, cannot parse.",str);
+             return(msg);
+         }
+         port_h = port_l+256*port_h;
+         host[l]=0;
+         for(;l>0;l--)
+             if(host[l]==',')
+                 host[l]='.';
+     }     
+ }
 
- server_data=OpenClientSocket(host,port_l+256*port_h,ConnectTimeout);
+ server_data=OpenClientSocket(host, port_h,ConnectTimeout);
  init_buffer(server_data);
 
  if(server_data==-1)
