--- curl-7.19.6/lib/hostthre.c	Thu Apr 23 04:51:08 2009
+++ costa/lib/hostthre.c	Fri Sep 11 14:52:46 2009
@@ -94,83 +94,149 @@
 
 #ifdef CURLRES_IPV4
   #define THREAD_FUNC  gethostbyname_thread
+  #define COMPLETE_FUNC gethostbyname_complete
   #define THREAD_NAME "gethostbyname_thread"
 #else
   #define THREAD_FUNC  getaddrinfo_thread
+  #define COMPLETE_FUNC getaddrinfo_complete
   #define THREAD_NAME "getaddrinfo_thread"
 #endif
 
-struct thread_data {
-  HANDLE thread_hnd;
-  unsigned thread_id;
-  DWORD  thread_status;
-  curl_socket_t dummy_sock;   /* dummy for Curl_resolv_fdset() */
-  HANDLE mutex_waiting;  /* marks that we are still waiting for a resolve */
-  HANDLE event_resolved; /* marks that the thread obtained the information */
-  HANDLE event_thread_started; /* marks that the thread has initialized and
-                                  started */
-  HANDLE mutex_terminate; /* serializes access to flag_terminate */
-  HANDLE event_terminate; /* flag for thread to terminate instead of calling
-                             callbacks */
+/* Data for synchronization between resolver thread and its parent */
+struct thread_sync_data {
+  char * hostname;        /* hostname to resolve, Curl_async.hostname
+                             duplicate */
+  int port;
+  int sock_error;
+  int rc;
+  struct hostent he;
+  Curl_addrinfo *res;
 #ifdef CURLRES_IPV6
   struct addrinfo hints;
 #endif
 };
 
-/* Data for synchronization between resolver thread and its parent */
-struct thread_sync_data {
-  HANDLE mutex_waiting;   /* thread_data.mutex_waiting duplicate */
-  HANDLE mutex_terminate; /* thread_data.mutex_terminate duplicate */
-  HANDLE event_terminate; /* thread_data.event_terminate duplicate */
-  char * hostname;        /* hostname to resolve, Curl_async.hostname
-                             duplicate */
+struct thread_data {
+  HANDLE thread_hnd;
+  curl_socket_t dummy_sock;
+  unsigned int poll_interval;
+  int interval_end;
+  struct thread_sync_data tsd;
 };
 
+static void hostent_dup_free(struct hostent *dst)
+{
+  int i;
+
+  if (dst->h_name) {
+    free(dst->h_name);
+    dst->h_name = NULL;
+  }
+
+  if (dst->h_aliases) {
+    for (i = 0; ; ++i) {
+      if (dst->h_aliases[i])
+        free(dst->h_aliases[i]);
+      else
+        break;
+    }
+    free(dst->h_aliases);
+    dst->h_aliases = NULL;
+  }
+
+  if (dst->h_addr_list) {
+    for(i = 0; ; ++i) {
+      if (dst->h_addr_list[i])
+        free(dst->h_addr_list[i]);
+      else
+        break;
+    }
+    free(dst->h_addr_list);
+    dst->h_addr_list = NULL;
+  }
+}
+
+static int hostent_dup(struct hostent *dst, struct hostent * src)
+{
+  int i, n;
+
+  if (src->h_name) {
+    dst->h_name = strdup(src->h_name);
+    if (!dst->h_name) goto err_exit;
+  } else
+    dst->h_name = 0;
+
+  if (src->h_aliases) {
+    for (n = 0; ; ++n) {
+      if (!src->h_aliases[n])
+        break;
+    }
+    dst->h_aliases = malloc((n + 1) * sizeof(char *));
+    if (!dst->h_aliases) goto err_exit;
+ 
+    for (i = 0; i < n; ++i) {
+      dst->h_aliases[i] = strdup(src->h_aliases[i]);
+      if (!dst->h_aliases[i])
+        goto err_exit;
+    }
+    dst->h_aliases[n] = NULL;
+  } else 
+    dst->h_aliases = NULL;
+
+  dst->h_addrtype = src->h_addrtype;
+  dst->h_length = src->h_length;
+
+  if (src->h_addr_list) {
+    for (n = 0; ; ++n) {
+      if (!src->h_addr_list[n])
+        break;
+    } 
+    dst->h_addr_list = malloc((n + 1) * sizeof(char *));
+    if (!dst->h_addr_list) goto err_exit;
+
+    for (i = 0; i < n; ++i) {
+      dst->h_addr_list[i] = malloc(src->h_length);
+      if (!dst->h_addr_list[i]) goto err_exit;
+
+      memcpy(dst->h_addr_list[i], src->h_addr_list[i], src->h_length); 
+    } 
+    dst->h_addr_list[n] = NULL;
+  } else {
+    dst->h_addr_list = NULL;
+  }
+  return CURLE_OK;
+
+err_exit:
+  hostent_dup_free(dst);
+  return CURLE_OUT_OF_MEMORY;
+}
+
+
 /* Destroy resolver thread synchronization data */
 static
 void destroy_thread_sync_data(struct thread_sync_data * tsd)
 {
   if(tsd->hostname)
     free(tsd->hostname);
-  if(tsd->event_terminate)
-    CloseHandle(tsd->event_terminate);
-  if(tsd->mutex_terminate)
-    CloseHandle(tsd->mutex_terminate);
-  if(tsd->mutex_waiting)
-    CloseHandle(tsd->mutex_waiting);
+  
+  hostent_dup_free(&tsd->he);
+
+  if (tsd->res)
+    Curl_freeaddrinfo(tsd->res);
+
   memset(tsd,0,sizeof(*tsd));
 }
 
 /* Initialize resolver thread synchronization data */
 static
-BOOL init_thread_sync_data(struct thread_data * td,
+BOOL init_thread_sync_data(
                            const char * hostname,
                            struct thread_sync_data * tsd)
 {
-  HANDLE curr_proc = GetCurrentProcess();
-
   memset(tsd, 0, sizeof(*tsd));
-  if(!DuplicateHandle(curr_proc, td->mutex_waiting,
-                       curr_proc, &tsd->mutex_waiting, 0, FALSE,
-                       DUPLICATE_SAME_ACCESS)) {
-    /* failed to duplicate the mutex, no point in continuing */
-    destroy_thread_sync_data(tsd);
-    return FALSE;
-  }
-  if(!DuplicateHandle(curr_proc, td->mutex_terminate,
-                       curr_proc, &tsd->mutex_terminate, 0, FALSE,
-                       DUPLICATE_SAME_ACCESS)) {
-    /* failed to duplicate the mutex, no point in continuing */
-    destroy_thread_sync_data(tsd);
-    return FALSE;
-  }
-  if(!DuplicateHandle(curr_proc, td->event_terminate,
-                       curr_proc, &tsd->event_terminate, 0, FALSE,
-                       DUPLICATE_SAME_ACCESS)) {
-    /* failed to duplicate the event, no point in continuing */
-    destroy_thread_sync_data(tsd);
-    return FALSE;
-  }
+
+  tsd->sock_error = CURL_ASYNC_SUCCESS;
+
   /* Copying hostname string because original can be destroyed by parent
    * thread during gethostbyname execution.
    */
@@ -183,157 +249,72 @@
   return TRUE;
 }
 
-/* acquire resolver thread synchronization */
-static
-BOOL acquire_thread_sync(struct thread_sync_data * tsd)
-{
-  /* is the thread initiator still waiting for us ? */
-  if(WaitForSingleObject(tsd->mutex_waiting, 0) == WAIT_TIMEOUT) {
-    /* yes, it is */
-
-    /* Waiting access to event_terminate */
-    if(WaitForSingleObject(tsd->mutex_terminate, INFINITE) != WAIT_OBJECT_0) {
-      /* Something went wrong - now just ignoring */
-    }
-    else {
-      if(WaitForSingleObject(tsd->event_terminate, 0) != WAIT_TIMEOUT) {
-        /* Parent thread signaled us to terminate.
-         * This means that all data in conn->async is now destroyed
-         * and we cannot use it.
-         */
-      }
-      else {
-        return TRUE;
-      }
-    }
-  }
-  return FALSE;
-}
-
-/* release resolver thread synchronization */
-static
-void release_thread_sync(struct thread_sync_data * tsd)
-{
-  ReleaseMutex(tsd->mutex_terminate);
-}
-
 #if defined(CURLRES_IPV4)
+
 /*
- * gethostbyname_thread() resolves a name, calls the Curl_addrinfo4_callback
- * and then exits.
+ * gethostbyname_thread() resolves a name and then exits.
  *
  * For builds without ARES/ENABLE_IPV6, create a resolver thread and wait on
  * it.
  */
-static unsigned __stdcall gethostbyname_thread (void *arg)
+static unsigned int __stdcall gethostbyname_thread (void *arg)
 {
-  struct connectdata *conn = (struct connectdata*) arg;
-  struct thread_data *td = (struct thread_data*) conn->async.os_specific;
-  struct hostent *he;
-  int    rc = 0;
-
-  /* Duplicate the passed mutex and event handles.
-   * This allows us to use it even after the container gets destroyed
-   * due to a resolver timeout.
-   */
-  struct thread_sync_data tsd = { 0,0,0,NULL };
+  struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
+  struct hostent *he = gethostbyname (tsd->hostname);
 
-  if(!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
-    /* thread synchronization data initialization failed */
-    return (unsigned)-1;
-  }
+  if (!he)
+    tsd->sock_error = SOCKERRNO;
+  else
+    tsd->rc = hostent_dup(&tsd->he, he);
 
-  conn->async.status = NO_DATA;  /* pending status */
-  SET_SOCKERRNO(conn->async.status);
-
-  /* Signaling that we have initialized all copies of data and handles we
-     need */
-  SetEvent(td->event_thread_started);
+  return 0;
+}
 
-  he = gethostbyname (tsd.hostname);
 
-  /* is parent thread waiting for us and are we able to access conn members? */
-  if(acquire_thread_sync(&tsd)) {
-    /* Mark that we have obtained the information, and that we are calling
-     * back with it. */
-    SetEvent(td->event_resolved);
-    if(he) {
-      rc = Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
-    }
-    else {
-      rc = Curl_addrinfo4_callback(conn, SOCKERRNO, NULL);
-    }
-    release_thread_sync(&tsd);
-  }
+static void gethostbyname_complete(struct connectdata * conn)
+{
+  struct thread_sync_data *tsd = &(((struct thread_data *)conn->async.os_specific)->tsd);
 
-  /* clean up */
-  destroy_thread_sync_data(&tsd);
+  if (tsd->rc != 0)
+    return;
 
-  return (rc);
-  /* An implicit _endthreadex() here */
+  tsd->rc = Curl_addrinfo4_callback(conn, tsd->sock_error, &tsd->he);
 }
 
 #elif defined(CURLRES_IPV6)
 
 /*
- * getaddrinfo_thread() resolves a name, calls Curl_addrinfo6_callback and then
- * exits.
+ * getaddrinfo_thread() resolves a name and then exits.
  *
  * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  * and wait on it.
  */
-static unsigned __stdcall getaddrinfo_thread (void *arg)
+static unsigned int __stdcall getaddrinfo_thread (void *arg)
 {
-  struct connectdata *conn = (struct connectdata*) arg;
-  struct thread_data *td   = (struct thread_data*) conn->async.os_specific;
-  Curl_addrinfo      *res;
+  struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
   char   service [NI_MAXSERV];
-  int    rc;
-  struct addrinfo hints = td->hints;
 
-  /* Duplicate the passed mutex handle.
-   * This allows us to use it even after the container gets destroyed
-   * due to a resolver timeout.
-   */
-  struct thread_sync_data tsd = { 0,0,0,NULL };
-
-  if(!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
-    /* thread synchronization data initialization failed */
-    return -1;
-  }
+  itoa(tsd->port, service, 10);
 
-  itoa(conn->async.port, service, 10);
+  tsd->rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
 
-  conn->async.status = NO_DATA;  /* pending status */
-  SET_SOCKERRNO(conn->async.status);
+  if (tsd->rc != 0)
+    tsd->sock_error = SOCKERRNO; 
 
-  /* Signaling that we have initialized all copies of data and handles we
-     need */
-  SetEvent(td->event_thread_started);
-
-  rc = Curl_getaddrinfo_ex(tsd.hostname, service, &hints, &res);
-
-  /* is parent thread waiting for us and are we able to access conn members? */
-  if(acquire_thread_sync(&tsd)) {
-    /* Mark that we have obtained the information, and that we are calling
-       back with it. */
-    SetEvent(td->event_resolved);
-
-    if(rc == 0) {
-      rc = Curl_addrinfo6_callback(conn, CURL_ASYNC_SUCCESS, res);
-    }
-    else {
-      rc = Curl_addrinfo6_callback(conn, SOCKERRNO, NULL);
-    }
-    release_thread_sync(&tsd);
-  }
+  return 0;
+}
 
-  /* clean up */
-  destroy_thread_sync_data(&tsd);
+static void getaddrinfo_complete(struct connectdata *conn)
+{
+  struct thread_sync_data *tsd = &(((struct thread_data *)conn->async.os_specific)->tsd);
 
-  return (rc);
-  /* An implicit _endthreadex() here */
+  tsd->rc = Curl_addrinfo6_callback(conn, tsd->sock_error, tsd->res); 
+  /* The tsd->res structure has been copied to async.dns and perhaps the DNS cache.
+     Set our copy to NULL so destroy_thread_sync_data doesn't free it.
+   */
+  tsd->res = NULL;
 }
+
 #endif
 
 /*
@@ -347,36 +328,15 @@
 
   if(async->os_specific) {
     struct thread_data *td = (struct thread_data*) async->os_specific;
-    curl_socket_t sock = td->dummy_sock;
-
-    if(td->mutex_terminate && td->event_terminate) {
-      /* Signaling resolver thread to terminate */
-      if(WaitForSingleObject(td->mutex_terminate, INFINITE) == WAIT_OBJECT_0) {
-        SetEvent(td->event_terminate);
-        ReleaseMutex(td->mutex_terminate);
-      }
-      else {
-        /* Something went wrong - just ignoring it */
-      }
-    }
 
-    if(td->mutex_terminate)
-      CloseHandle(td->mutex_terminate);
-    if(td->event_terminate)
-      CloseHandle(td->event_terminate);
-    if(td->event_thread_started)
-      CloseHandle(td->event_thread_started);
-
-    if(sock != CURL_SOCKET_BAD)
-      sclose(sock);
-
-    /* destroy the synchronization objects */
-    if(td->mutex_waiting)
-      CloseHandle(td->mutex_waiting);
-    td->mutex_waiting = NULL;
-    if(td->event_resolved)
-      CloseHandle(td->event_resolved);
+    if (td->dummy_sock != CURL_SOCKET_BAD)
+      sclose(td->dummy_sock);
 
+    if (td->thread_hnd != INVALID_HANDLE_VALUE)
+      WaitForSingleObject(td->thread_hnd, INFINITE);
+ 
+    destroy_thread_sync_data(&td->tsd);
+    
     if(td->thread_hnd)
       CloseHandle(td->thread_hnd);
 
@@ -397,13 +357,18 @@
                                  const struct addrinfo *hints)
 {
   struct thread_data *td = calloc(sizeof(*td), 1);
-  HANDLE thread_and_event[2] = {0};
 
   if(!td) {
     SET_ERRNO(ENOMEM);
     return FALSE;
   }
 
+  if (!init_thread_sync_data(hostname, &td->tsd)) {
+    free(td);
+    SET_ERRNO(ENOMEM);
+    return FALSE;
+  }
+
   Curl_safefree(conn->async.hostname);
   conn->async.hostname = strdup(hostname);
   if(!conn->async.hostname) {
@@ -419,65 +384,21 @@
   conn->async.os_specific = (void*) td;
   td->dummy_sock = CURL_SOCKET_BAD;
 
-  /* Create the mutex used to inform the resolver thread that we're
-   * still waiting, and take initial ownership.
-   */
-  td->mutex_waiting = CreateMutex(NULL, TRUE, NULL);
-  if(td->mutex_waiting == NULL) {
-    Curl_destroy_thread_data(&conn->async);
-    SET_ERRNO(EAGAIN);
-    return FALSE;
-  }
-
-  /* Create the event that the thread uses to inform us that it's
-   * done resolving. Do not signal it.
-   */
-  td->event_resolved = CreateEvent(NULL, TRUE, FALSE, NULL);
-  if(td->event_resolved == NULL) {
-    Curl_destroy_thread_data(&conn->async);
-    SET_ERRNO(EAGAIN);
-    return FALSE;
-  }
-  /* Create the mutex used to serialize access to event_terminated
-   * between us and resolver thread.
-   */
-  td->mutex_terminate = CreateMutex(NULL, FALSE, NULL);
-  if(td->mutex_terminate == NULL) {
-    Curl_destroy_thread_data(&conn->async);
-    SET_ERRNO(EAGAIN);
-    return FALSE;
-  }
-  /* Create the event used to signal thread that it should terminate.
-   */
-  td->event_terminate = CreateEvent(NULL, TRUE, FALSE, NULL);
-  if(td->event_terminate == NULL) {
-    Curl_destroy_thread_data(&conn->async);
-    SET_ERRNO(EAGAIN);
-    return FALSE;
-  }
-  /* Create the event used by thread to inform it has initialized its own data.
-   */
-  td->event_thread_started = CreateEvent(NULL, TRUE, FALSE, NULL);
-  if(td->event_thread_started == NULL) {
-    Curl_destroy_thread_data(&conn->async);
-    SET_ERRNO(EAGAIN);
-    return FALSE;
-  }
+  td->tsd.port = port;
+#ifdef CURLRES_IPV6
+  DEBUGASSERT(hints);
+  td->tsd.hints = *hints;
+#else
+  (void) hints;
+#endif
 
 #ifdef _WIN32_WCE
   td->thread_hnd = (HANDLE) CreateThread(NULL, 0,
                                          (LPTHREAD_START_ROUTINE) THREAD_FUNC,
-                                         conn, 0, &td->thread_id);
+                                         &td->tsd, 0, NULL);
 #else
   td->thread_hnd = (HANDLE) _beginthreadex(NULL, 0, THREAD_FUNC,
-                                           conn, 0, &td->thread_id);
-#endif
-
-#ifdef CURLRES_IPV6
-  DEBUGASSERT(hints);
-  td->hints = *hints;
-#else
-  (void) hints;
+                                         &td->tsd, 0, NULL);
 #endif
 
   if(!td->thread_hnd) {
@@ -487,21 +408,10 @@
      Curl_destroy_thread_data(&conn->async);
      return FALSE;
   }
-  /* Waiting until the thread will initialize its data or it will exit due errors.
-   */
-  thread_and_event[0] = td->thread_hnd;
-  thread_and_event[1] = td->event_thread_started;
-  if(WaitForMultipleObjects(sizeof(thread_and_event) /
-                             sizeof(thread_and_event[0]),
-                             (const HANDLE*)thread_and_event, FALSE,
-                             INFINITE) == WAIT_FAILED) {
-    /* The resolver thread has been created,
-     * most probably it works now - ignoring this "minor" error
-     */
-  }
+
   /* This socket is only to keep Curl_resolv_fdset() and select() happy;
-   * should never become signalled for read/write since it's unbound but
-   * Windows needs atleast 1 socket in select().
+   * should never become signalled for read since it's unbound but
+   * Windows needs at least 1 socket in select().
    */
   td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
   return TRUE;
@@ -521,84 +431,43 @@
 {
   struct thread_data   *td = (struct thread_data*) conn->async.os_specific;
   struct SessionHandle *data = conn->data;
-  long   timeout;
   DWORD  status;
   CURLcode rc;
 
   DEBUGASSERT(conn && td);
 
-  /* now, see if there's a connect timeout or a regular timeout to
-     use instead of the default one */
-  timeout =
-    conn->data->set.connecttimeout ? conn->data->set.connecttimeout :
-    conn->data->set.timeout ? conn->data->set.timeout :
-    CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
-
   /* wait for the thread to resolve the name */
-  status = WaitForSingleObject(td->event_resolved, 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;
-
-  /* close the event handle, it's useless now */
-  CloseHandle(td->event_resolved);
-  td->event_resolved = NULL;
-
-  /* has the resolver thread succeeded in resolving our query ? */
-  if(status == WAIT_OBJECT_0) {
-    /* wait for the thread to exit, it's in the callback sequence */
-    if(WaitForSingleObject(td->thread_hnd, 5000) == WAIT_TIMEOUT) {
-      TerminateThread(td->thread_hnd, 0);
-      conn->async.done = TRUE;
-      td->thread_status = (DWORD)-1;
-    }
-    else {
-      /* Thread finished before timeout; propagate Winsock error to this
-       * thread.  'conn->async.done = TRUE' is set in
-       * Curl_addrinfo4/6_callback().
-       */
-      SET_SOCKERRNO(conn->async.status);
-      GetExitCodeThread(td->thread_hnd, &td->thread_status);
-    }
-  }
-  else {
-    conn->async.done = TRUE;
-    td->thread_status = (DWORD)-1;
+  status = WaitForSingleObject(td->thread_hnd, INFINITE);
+  if (status == WAIT_OBJECT_0) {
+    COMPLETE_FUNC(conn);
+    rc = td->tsd.rc;
+  } else {
+    /* Weird error happened */
+    rc = -1;
+    failf(data, "Unexpected error from Windows: %d %d", status, GetLastError());
   }
 
+  conn->async.done = TRUE;
+    
   if(entry)
     *entry = conn->async.dns;
 
-  rc = CURLE_OK;
-
   if(!conn->async.dns) {
     /* a name was not resolved */
-    if(td->thread_status == CURLE_OUT_OF_MEMORY) {
-      rc = CURLE_OUT_OF_MEMORY;
+    if(rc == CURLE_OUT_OF_MEMORY) {
       failf(data, "Could not resolve host: %s", curl_easy_strerror(rc));
-    }
-    else if(conn->async.done) {
-      if(conn->bits.httpproxy) {
-        failf(data, "Could not resolve proxy: %s; %s",
-              conn->proxy.dispname, Curl_strerror(conn, conn->async.status));
-        rc = CURLE_COULDNT_RESOLVE_PROXY;
-      }
-      else {
-        failf(data, "Could not resolve host: %s; %s",
-              conn->host.name, Curl_strerror(conn, conn->async.status));
-        rc = CURLE_COULDNT_RESOLVE_HOST;
-      }
-    }
-    else if(td->thread_status == (DWORD)-1 || conn->async.status == NO_DATA) {
+    } else if (rc == CURLE_OPERATION_TIMEDOUT) {
       failf(data, "Resolving host timed out: %s", conn->host.name);
-      rc = CURLE_OPERATION_TIMEDOUT;
+    } else if(conn->bits.httpproxy) {
+      failf(data, "Could not resolve proxy: %s; %s",
+            conn->proxy.dispname, Curl_strerror(conn, conn->async.status));
+      rc = CURLE_COULDNT_RESOLVE_PROXY;
+    }
+    else {
+      failf(data, "Could not resolve host: %s; %s",
+            conn->host.name, Curl_strerror(conn, conn->async.status));
+      rc = CURLE_COULDNT_RESOLVE_HOST;
     }
-    else
-      rc = CURLE_OPERATION_TIMEDOUT;
   }
 
   Curl_destroy_thread_data(&conn->async);
@@ -618,11 +487,15 @@
                           struct Curl_dns_entry **entry)
 {
   struct SessionHandle *data = conn->data;
+  struct thread_data   *td = (struct thread_data*) conn->async.os_specific;
+  DWORD st;
 
   *entry = NULL;
 
-  if(conn->async.done) {
-    /* we're done */
+  st = WaitForSingleObject(td->thread_hnd, 0);
+  if (st == WAIT_OBJECT_0) { 
+    conn->async.done = TRUE;
+    COMPLETE_FUNC(conn);
     Curl_destroy_thread_data(&conn->async);
     if(!conn->async.dns) {
       failf(data, "Could not resolve host: %s; %s",
@@ -630,7 +503,34 @@
       return CURLE_COULDNT_RESOLVE_HOST;
     }
     *entry = conn->async.dns;
+  } else if (td) {
+    /* poll for name lookup done with exponential backoff up to 250ms */
+    int elapsed;
+
+    elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
+    if (elapsed < 0) {
+      elapsed = 0;
+    }
+
+    if (td->poll_interval == 0) {
+      /* Start at 1ms poll interval */
+      td->poll_interval = 1;
+    } else if (elapsed >= td->interval_end) {
+      /* Back-off exponentially if last interval expired  */
+      td->poll_interval *= 2;
+    }
+
+    if (td->poll_interval > 250)
+      td->poll_interval = 250;
+
+    td->interval_end = elapsed + td->poll_interval;
+
+    Curl_expire(conn->data, td->poll_interval);
+  } else {
+    failf(conn->data, "Curl_is_resolved: shouldn't get here");
+    return CURLE_COULDNT_RESOLVE_HOST;
   }
+
   return CURLE_OK;
 }
 
@@ -643,10 +543,10 @@
 
   if(td && td->dummy_sock != CURL_SOCKET_BAD) {
     if(numsocks) {
-      /* return one socket waiting for writable, even though this is just
+      /* return one socket waiting for readable, even though this is just
          a dummy */
       socks[0] = td->dummy_sock;
-      return GETSOCK_WRITESOCK(0);
+      return GETSOCK_READSOCK(0);
     }
   }
   return 0;
