From 59eaac47bdca2cf641165476121a98c5a49b9e15 Mon Sep 17 00:00:00 2001
From: Gokhan Sengun <gokhansengun@gmail.com>
Date: Mon, 25 Jun 2012 02:06:50 +0300
Subject: [PATCH 2/2] ftp: active conn, place calling sockopt callback at the
 end of function

Commit b91d29a28e170c16d65d956db79f2cd3a82372d2 introduces a bug and breaks Curl_closesocket function. sock_accepted flag for the second socket should be tagged as TRUE before the sockopt callback is called because in case the callback returns an error, Curl_closesocket function is going to call the - fclosesocket - callback for the accept()ed socket
---
 lib/ftp.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/ftp.c b/lib/ftp.c
index d0eee7c..dd2310d 100644
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -348,6 +348,10 @@ static CURLcode AcceptServerConnect(struct connectdata *conn)
   }
   infof(data, "Connection accepted from server\n");
 
+  conn->sock[SECONDARYSOCKET] = s;
+  curlx_nonblock(s, TRUE); /* enable non-blocking */
+  conn->sock_accepted[SECONDARYSOCKET] = TRUE;
+
   if(data->set.fsockopt) {
     int error = 0;
 
@@ -358,13 +362,11 @@ static CURLcode AcceptServerConnect(struct connectdata *conn)
 
     if(error) {
       Curl_closesocket(conn, s); /* close the socket and bail out */
+      conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
       return CURLE_ABORTED_BY_CALLBACK;
     }
   }
 
-  conn->sock[SECONDARYSOCKET] = s;
-  curlx_nonblock(s, TRUE); /* enable non-blocking */
-  conn->sock_accepted[SECONDARYSOCKET] = TRUE;
   return CURLE_OK;
 
 }
-- 
1.7.9.5

