--- hostthre.c-ch1	2004-10-13 19:24:37.009915200 +0200
+++ hostthre.c	2004-10-15 16:42:12.087744000 +0200
@@ -178,11 +178,15 @@
   struct hostent *he;
   int    rc;
 
-  /* Save the passed mutex handle to local storage.
+  /* Duplicate the passed mutex handle.
    * This allows us to use it even after the container gets destroyed
    * due to a resolver timeout.
    */
-  HANDLE mutex_waiting = td->mutex_waiting;
+  HANDLE mutex_waiting = NULL;
+  if( FALSE == DuplicateHandle( GetCurrentProcess(), td->mutex_waiting, GetCurrentProcess(), &mutex_waiting, 0, FALSE, DUPLICATE_SAME_ACCESS ) ) {
+	  /* failed to duplicate the mutex, no point in continuing */
+	  return 0;
+  }
 
   /* Sharing the same _iob[] element with our parent thread should
    * hopefully make printouts synchronised. I'm not sure it works
@@ -203,18 +207,21 @@
      */
     SetEvent( td->event_resolved );
 
-	if (he) {
-		Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
-		rc = 1;
-	}
-	else {
-		Curl_addrinfo4_callback(conn, (int)WSAGetLastError(), NULL);
-		rc = 0;
-	}
-	TRACE(("Winsock-error %d, addr %s\n", conn->async.status,
-			he ? inet_ntoa(*(struct in_addr*)he->h_addr) : "unknown"));
+    if (he) {
+        Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
+        rc = 1;
+    }
+    else {
+        Curl_addrinfo4_callback(conn, (int)WSAGetLastError(), NULL);
+        rc = 0;
+    }
+    TRACE(("Winsock-error %d, addr %s\n", conn->async.status,
+            he ? inet_ntoa(*(struct in_addr*)he->h_addr) : "unknown"));
   }
 
+  /* clean up */
+  CloseHandle( mutex_waiting );
+
   return (rc);
   /* An implicit _endthreadex() here */
 }
@@ -236,11 +243,15 @@
   char   service [NI_MAXSERV];
   int    rc;
 
-  /* Save the passed mutex handle to local storage.
+  /* Duplicate the passed mutex handle.
    * This allows us to use it even after the container gets destroyed
    * due to a resolver timeout.
    */
-  HANDLE mutex_waiting = td->mutex_waiting;
+  HANDLE mutex_waiting = NULL;
+  if( FALSE == DuplicateHandle( GetCurrentProcess(), td->mutex_waiting, GetCurrentProcess(), &mutex_waiting, 0, FALSE, DUPLICATE_SAME_ACCESS ) ) {
+	  /* failed to duplicate the mutex, no point in continuing */
+	  return 0;
+  }
 
   *stderr = *td->stderr_file;
 
@@ -273,6 +284,9 @@
     }
   }
 
+  /* clean up */
+  CloseHandle( mutex_waiting );
+
   return (rc);
   /* An implicit _endthreadex() here */
 }
@@ -408,6 +422,8 @@
   status = WaitForSingleObject( td->event_resolved, 1000UL*timeout );
 
   /* mark that we are now done waiting */
+  ReleaseMutex( td->mutex_waiting );
+  /* close our handle to the mutex, no point in hanging on to it */
   CloseHandle( td->mutex_waiting );
   td->mutex_waiting = NULL;
 
