Index: lib/ftp.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/ftp.c,v
retrieving revision 1.327
diff -u -r1.327 ftp.c
--- lib/ftp.c	21 Jul 2005 22:18:35 -0000	1.327
+++ lib/ftp.c	16 Aug 2005 13:11:44 -0000
@@ -2015,16 +2015,17 @@
     result = AllowServerConnect(conn);
     if( result )
       return result;
-  }
 
-  if(conn->ssl[SECONDARYSOCKET].use) {
-    /* since we only have a plaintext TCP connection here, we must now
-       do the TLS stuff */
-    infof(data, "Doing the SSL/TLS handshake on the data stream\n");
-    /* BLOCKING */
-    result = Curl_ssl_connect(conn, SECONDARYSOCKET);
-    if(result)
-      return result;
+    /* If PASV is used, this is is made elsewhere */
+    if(conn->ssl[SECONDARYSOCKET].use) {
+      /* since we only have a plaintext TCP connection here, we must now
+         do the TLS stuff */
+      infof(data, "Doing the SSL/TLS handshake on the data stream\n");
+      /* BLOCKING */
+      result = Curl_ssl_connect(conn, SECONDARYSOCKET);
+      if(result)
+        return result;
+    }
   }
 
   *(ftp->bytecountp)=0;
@@ -3096,6 +3097,18 @@
   if(!ftp->no_transfer && !conn->bits.no_body) {
     /* a transfer is about to take place */
 
+    if(conn->ssl[SECONDARYSOCKET].use &&
+       !data->set.ftp_use_port) {
+      /* PASV is used and we just got the data connection connected, then
+         it is time to handshake the secure stuff. */
+
+      infof(data, "Doing the SSL/TLS handshake on the data stream\n");
+      /* BLOCKING */
+      result = Curl_ssl_connect(conn, SECONDARYSOCKET);
+      if(result)
+        return result;
+    }
+
     if(data->set.upload) {
       NBFTPSENDF(conn, "TYPE %c", data->set.ftp_ascii?'A':'I');
       state(conn, FTP_STOR_TYPE);
