diff -u3 -Hb -r CVS-latest/lib/hostip6.c lib/hostip6.c
--- CVS-latest/lib/hostip6.c	Mon Apr 26 09:20:11 2004
+++ lib/hostip6.c	Mon Apr 26 13:02:47 2004
@@ -204,7 +204,7 @@
   if(data->set.ip_version == CURL_IPRESOLVE_V6) {
     /* see if we have an IPv6 stack */
     curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
-    if (s < 0)
+    if (s != CURL_SOCKET_BAD)
       /* an ipv6 address was requested and we can't get/use one */
       return FALSE;
     sclose(s);
diff -u3 -Hb -r CVS-latest/lib/hostthre.c lib/hostthre.c
--- CVS-latest/lib/hostthre.c	Mon Apr 26 09:20:11 2004
+++ lib/hostthre.c	Mon Apr 26 15:11:22 2004
@@ -83,9 +83,7 @@
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>

-#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
-#include "inet_ntoa_r.h"
-#endif
+#include "inet_ntop.h"

 /* The last #include file should be: */
 #ifdef CURLDEBUG
@@ -137,17 +135,14 @@
 #endif

 #ifdef DEBUG_THREADING_GETADDRINFO
-
-/* inet_ntop.c */
-extern const char *Curl_inet_ntop (int af, const void *addr, char *buf, size_t size);
-
 static void dump_addrinfo (struct connectdata *conn, const struct addrinfo *ai)
 {
   TRACE(("dump_addrinfo:\n"));
   for ( ; ai; ai = ai->ai_next) {
     char  buf [INET6_ADDRSTRLEN];
+
     trace_it("    fam %2d, CNAME %s, ",
-             af, ai->ai_canonname ? ai->ai_canonname : "<none>");
+             ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
     if (Curl_printable_address(ai->ai_family, ai->ai_addr, buf, sizeof(buf)))
       trace_it("%s\n", buf);
     else
@@ -499,7 +498,7 @@

   /* see if we have an IPv6 stack */
   s = socket(PF_INET6, SOCK_DGRAM, 0);
-  if (s < 0) {
+  if (s != CURL_SOCKET_BAD) {
     /* Some non-IPv6 stacks have been found to make very slow name resolves
      * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
      * the stack seems to be a non-ipv6 one. */
diff -u3 -Hb -r CVS-latest/lib/http.c lib/http.c
--- CVS-latest/lib/http.c	Fri Apr 23 12:37:52 2004
+++ lib/http.c	Mon Apr 26 15:02:42 2004
@@ -92,6 +92,7 @@
 #include "http_negotiate.h"
 #include "url.h"
 #include "share.h"
+#include "hostip.h"
 #include "http.h"

 #define _MPRINTF_REPLACE /* use our functions only */
@@ -252,7 +253,7 @@
      host due to a location-follow, we do some weirdo checks here */
   if(!data->state.this_is_a_follow ||
      !data->state.auth_host ||
-     curl_strequal(data->state.auth_host, conn->hostname) ||
+     curl_strequal(data->state.auth_host, TRUE_HOSTNAME(conn)) ||
      data->set.http_disable_hostname_check_before_authentication) {

   /* Send proxy authentication header if needed */
@@ -1112,7 +1113,7 @@

     /* either HTTPS over proxy, OR explicitly asked for a tunnel */
     result = Curl_ConnectHTTPProxyTunnel(conn, FIRSTSOCKET,
-                                         conn->hostname, conn->remote_port);
+                                         TRUE_HOSTNAME(conn), conn->remote_port);
     if(CURLE_OK != result)
       return result;
   }
@@ -1131,7 +1132,7 @@
       /* Free to avoid leaking memory on multiple requests*/
       free(data->state.auth_host);

-    data->state.auth_host = strdup(conn->hostname);
+    data->state.auth_host = strdup(TRUE_HOSTNAME(conn));
   }

   return CURLE_OK;
@@ -1218,7 +1219,7 @@
   struct HTTP *http;
   struct Cookie *co=NULL; /* no cookies from start */
   char *ppath = conn->path;
-  char *host = conn->hostname;
+  char *host = TRUE_HOSTNAME(conn);
   const char *te = ""; /* tranfer-encoding */
   char *ptr;
   char *request;
diff -u3 -Hb -r CVS-latest/lib/url.c lib/url.c
--- CVS-latest/lib/url.c	Mon Apr 26 09:20:11 2004
+++ lib/url.c	Mon Apr 26 15:29:02 2004
@@ -153,7 +153,6 @@

 #ifdef USE_LIBIDN
 static bool is_ASCII_name (const char *hostname);
-static bool is_ACE_name (const char *hostname);
 #endif

 #ifndef USE_ARES
@@ -1373,6 +1372,9 @@
   Curl_safefree(conn->newurl);
   Curl_safefree(conn->pathbuffer); /* the URL path buffer */
   Curl_safefree(conn->namebuffer); /* the URL host name buffer */
+#ifdef USE_LIBIDN
+  Curl_safefree(conn->ace_hostname);
+#endif
   Curl_SSL_Close(conn);

   /* close possibly still open sockets */
@@ -1473,7 +1475,7 @@
         continue;

       if(strequal(needle->protostr, check->protostr) &&
-         strequal(needle->hostname, check->hostname) &&
+         strequal(TRUE_HOSTNAME(needle), TRUE_HOSTNAME(check)) &&
          (needle->remote_port == check->remote_port) ) {
         if(needle->protocol & PROT_SSL) {
           /* This is SSL, verify that we're using the same
@@ -1763,7 +1765,7 @@
 #ifndef ENABLE_IPV6
     struct Curl_dns_entry *dns;
     Curl_addrinfo *hp=NULL;
-    int rc = Curl_resolv(conn, conn->hostname, conn->remote_port, &dns);
+    int rc = Curl_resolv(conn, TRUE_HOSTNAME(conn), conn->remote_port, &dns);

     if(rc == -1)
       return 1;
@@ -2917,6 +2919,10 @@
     free(conn->namebuffer); /* free the newly allocated name buffer */
     conn->namebuffer = old_conn->namebuffer; /* use the old one */
     conn->hostname = old_conn->hostname;
+#ifdef USE_LIBIDN
+    Curl_safefree(conn->ace_hostname);
+    conn->ace_hostname = old_conn->ace_hostname;
+#endif

     free(conn->pathbuffer); /* free the newly allocated path pointer */
     conn->pathbuffer = old_conn->pathbuffer; /* use the old one */
@@ -3042,6 +3048,30 @@
   }
 #endif /* USE_ARES */

+#ifdef USE_LIBIDN
+  /*************************************************************
+   * Check name for non-ASCII and convert hostname to ACE form.
+   *************************************************************/
+
+  if(!conn->bits.reuse && conn->remote_port) {
+    const char *host = conn->hostname;
+    char *ace_hostname = NULL;
+
+    if (strlen(host) < 64 && !is_ASCII_name(host)) {
+       int rc = idna_to_ascii_lz(host, &ace_hostname, 0);
+
+       if(IDNA_SUCCESS == rc && ace_hostname) {
+         conn->ace_hostname = strdup(ace_hostname);
+         (free)(ace_hostname);
+       }
+       else if(IDNA_CONTAINS_ACE_PREFIX == rc)
+         ;  /* already converted somehow (contains a "xn--" prefix) */
+       else
+         infof(data, "Failed to convert %s to ACE; IDNA error %d\n", host, rc);
+    }
+  }
+#endif
+
   /*************************************************************
    * Resolve the name of the server or proxy
    *************************************************************/
@@ -3057,7 +3087,7 @@
     conn->port =  conn->remote_port; /* it is the same port */

     /* Resolve target host right on */
-    rc = Curl_resolv(conn, conn->hostname, conn->port, &hostaddr);
+    rc = Curl_resolv(conn, TRUE_HOSTNAME(conn), conn->port, &hostaddr);
     if(rc == 1)
       *async = TRUE;

@@ -3145,26 +3175,6 @@
        a file:// transfer */
     return result;

-#ifdef USE_LIBIDN
-  /*************************************************************
-   * Check name for non-ASCII and convert hostname to ACE form.
-   *************************************************************/
-
-  if(!conn->bits.reuse && conn->remote_port) {
-    const char *host = conn->hostname;
-    char *ace_hostname;
-
-    if (!is_ASCII_name(host) && !is_ACE_name(host)) {
-       int rc = idna_to_ascii_lz (host, &ace_hostname, 0);
-
-       if (rc == IDNA_SUCCESS)
-          conn->ace_hostname = ace_hostname;
-       else
-          infof(data, "Failed to convert %s to ACE; IDNA error %d\n", host, rc);
-    }
-  }
-#endif
-
   /*************************************************************
    * Send user-agent to HTTP proxies even if the target protocol
    * isn't HTTP.
@@ -3499,18 +3509,17 @@
 }

 /*
- * Helpers for IDNA convertions. To do.
+ * Helpers for IDNA convertions.
  */
 #ifdef USE_LIBIDN
 static bool is_ASCII_name (const char *hostname)
 {
-  (void) hostname;
-  return (TRUE);
-}
+  const unsigned char *ch = (const unsigned char*)hostname;

-static bool is_ACE_name (const char *hostname)
-{
-  (void) hostname;
-  return (FALSE);
+  while (*ch) {
+    if (*ch++ > 0x80)
+      return FALSE;
+  }
+  return TRUE;
 }
 #endif

