Index: lib/url.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/url.c,v
retrieving revision 1.696
diff -U 20 -r1.696 url.c
--- lib/url.c	21 Jan 2008 23:48:58 -0000	1.696
+++ lib/url.c	29 Jan 2008 10:18:24 -0000
@@ -4397,53 +4397,58 @@
   return code;
 #else
   (void)conn;
   (void)protocol_done;
   return CURLE_OK;
 #endif
 }
 
 
 CURLcode Curl_done(struct connectdata **connp,
                    CURLcode status,  /* an error if this is called after an
                                         error was detected */
                    bool premature)
 {
   CURLcode result;
   struct connectdata *conn = *connp;
   struct SessionHandle *data = conn->data;
 
   Curl_expire(data, 0); /* stop timer */
 
-  if(conn->bits.done)
-    return CURLE_OK; /* Curl_done() has already been called */
-
-  conn->bits.done = TRUE; /* called just now! */
-
   if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) &&
      conn->readchannel_inuse)
     conn->readchannel_inuse = FALSE;
   if(Curl_removeHandleFromPipeline(data, conn->send_pipe) &&
      conn->writechannel_inuse)
     conn->writechannel_inuse = FALSE;
   Curl_removeHandleFromPipeline(data, conn->pend_pipe);
 
+  if(conn->bits.done ||
+     (conn->send_pipe->size + conn->recv_pipe->size != 0 &&
+      !data->set.reuse_forbid &&
+      !conn->bits.close))
+    /* Stop if Curl_done() has already been called or pipeline
+       is not empty and we do not have to close connection. */
+    return CURLE_OK;
+
+  conn->bits.done = TRUE; /* called just now! */
+
   /* Cleanup possible redirect junk */
   if(data->req.newurl) {
     free(data->req.newurl);
     data->req.newurl = NULL;
   }
 
   if(conn->dns_entry) {
     Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
     conn->dns_entry = NULL;
   }
 
   /* this calls the protocol-specific function pointer previously set */
   if(conn->handler->done)
     result = conn->handler->done(conn, status, premature);
   else
     result = CURLE_OK;
 
   Curl_pgrsDone(conn); /* done with the operation */
 
   /* if the transfer was completed in a paused state there can be buffered
