diff --git a/ChangeLog b/ChangeLog
index 5a666a25..402a5440 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2003-09-09  Hrvoje Niksic  <hniksic@xemacs.org>
+
+	* configure.in, aclocal.m4: Added configure check for IPv6 and
+	getaddrinfo.  From Daniel Stenberg.
+
 2003-09-05  Maciej W. Rozycki  <macro@ds2.pg.gda.pl>
 
 	* configure.in: Additional M4 quoting.
diff --git a/aclocal.m4 b/aclocal.m4
index 3406e42e..c0e40bdb 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -86,6 +86,47 @@ else
   AC_MSG_RESULT(no)
 fi])
 
+dnl ************************************************************
+dnl check for working getaddrinfo()
+dnl
+AC_DEFUN(WGET_CHECK_WORKING_GETADDRINFO,[
+  AC_CACHE_CHECK(for working getaddrinfo, ac_cv_working_getaddrinfo,[
+  AC_TRY_RUN( [
+#include <netdb.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+int main(void) {
+    struct addrinfo hints, *ai;
+    int error;
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    error = getaddrinfo("127.0.0.1", "8080", &hints, &ai);
+    if (error) {
+        exit(1);
+    }
+    else {
+        exit(0);
+    }
+}
+],[
+  ac_cv_working_getaddrinfo="yes"
+],[
+  ac_cv_working_getaddrinfo="no"
+],[
+  ac_cv_working_getaddrinfo="yes"
+])])
+if test x"$ac_cv_working_getaddrinfo" = xyes; then
+  AC_DEFINE(HAVE_GETADDRINFO, 1, [Define if getaddrinfo exists and works])
+  AC_DEFINE(ENABLE_IPV6, 1, [Define if you want to enable IPv6 support])
+
+  IPV6_ENABLED=1
+  AC_SUBST(IPV6_ENABLED)
+fi
+])
+
 
 # This code originates from Ulrich Drepper's AM_WITH_NLS.
 
diff --git a/configure.in b/configure.in
index 2166cc5d..55555b27 100644
--- a/configure.in
+++ b/configure.in
@@ -30,7 +30,7 @@ dnl Process this file with autoconf to produce a configure script.
 dnl
 
 AC_INIT(src/version.c)
-AC_PREREQ(2.12)
+AC_PREREQ(2.50)
 AC_CONFIG_HEADER(src/config.h)
 
 dnl
@@ -155,7 +155,6 @@ AC_C_CONST
 AC_C_INLINE
 AC_TYPE_SIZE_T
 AC_TYPE_PID_T
-dnl #### This generates a warning.  What do I do to shut it up?
 AC_C_BIGENDIAN
 
 # Check size of long.
@@ -442,6 +441,55 @@ fi
 AC_DEFINE(HAVE_MD5)
 AC_SUBST(MD5_OBJ)
 
+dnl **********************************************************************
+dnl Checks for IPv6
+dnl **********************************************************************
+
+dnl
+dnl If --enable-ipv6 is specified, we try to use IPv6 (as long as
+dnl getaddrinfo is also present).  If --disable-ipv6 is specified, we
+dnl don't use IPv6 or getaddrinfo.  If neither are specified, we test
+dnl whether it's possible to create an AF_INET6 socket and if yes, use
+dnl IPv6.
+dnl
+
+AC_MSG_CHECKING([whether to enable ipv6])
+AC_ARG_ENABLE(ipv6,
+AC_HELP_STRING([--enable-ipv6],[Enable ipv6 support])
+AC_HELP_STRING([--disable-ipv6],[Disable ipv6 support]),
+[ case "$enableval" in
+  no)
+       AC_MSG_RESULT(no)
+       ipv6=no
+       ;;
+  *)   AC_MSG_RESULT(yes)
+       ipv6=yes
+       ;;
+  esac ],
+
+  AC_TRY_RUN([ /* is AF_INET6 available? */
+#include <sys/types.h>
+#include <sys/socket.h>
+main()
+{
+ if (socket(AF_INET6, SOCK_STREAM, 0) < 0)
+   exit(1);
+ else
+   exit(0);
+}
+],
+  AC_MSG_RESULT(yes)
+  ipv6=yes,
+  AC_MSG_RESULT(no)
+  ipv6=no,
+  AC_MSG_RESULT(no)
+  ipv6=no
+))
+
+if test x"$ipv6" = xyes; then
+  WGET_CHECK_WORKING_GETADDRINFO
+fi
+
 dnl
 dnl Set of available languages.
 dnl
diff --git a/src/ChangeLog b/src/ChangeLog
index 15affbca..73201fb7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2003-09-09  Hrvoje Niksic  <hniksic@xemacs.org>
+
+	* config.h.in: Initialize HAVE_GETADDRINFO and ENABLE_IPV6.
+
+	* all: Use #ifdef ENABLE_IPV6 instead of the older INET6.  Use
+	HAVE_GETADDRINFO for getaddrinfo-related stuff.
+
 2003-09-09  Hrvoje Niksic  <hniksic@xemacs.org>
 
 	* url.c (url_parse): Return an error if the URL contains a [...]
diff --git a/src/config.h.in b/src/config.h.in
index dc5f37cb..0ee23e38 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -250,6 +250,12 @@ char *alloca ();
 /* Define if we're using builtin (GNU) md5.c.  */
 #undef HAVE_BUILTIN_MD5
 
+/* Define if you have the getaddrinfo function.  */
+#undef HAVE_GETADDRINFO
+
+/* Define if you want to enable the IPv6 support.  */
+#undef ENABLE_IPV6
+
 /* First a gambit to see whether we're on Solaris.  We'll
    need it below.  */
 #ifdef __sun
diff --git a/src/connect.c b/src/connect.c
index 8db28399..99f0909d 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -412,7 +412,7 @@ conaddr (int fd, ip_address *ip)
 
   switch (mysrv.sa.sa_family)
     {
-#ifdef INET6
+#ifdef ENABLE_IPV6
     case AF_INET6:
       memcpy (ip, &mysrv.sin6.sin6_addr, 16);
       return 1;
diff --git a/src/ftp-basic.c b/src/ftp-basic.c
index dd868a3a..5ba09527 100644
--- a/src/ftp-basic.c
+++ b/src/ftp-basic.c
@@ -252,7 +252,7 @@ ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
   return FTPOK;
 }
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
 uerr_t
 ftp_eprt (struct rbuf *rbuf)
 {
@@ -324,7 +324,7 @@ ftp_port (struct rbuf *rbuf)
 
   int nwritten;
   unsigned short port;
-#ifdef INET6
+#ifdef ENABLE_IPV6
   /*
     Only try the Extented Version if we actually use IPv6
   */
@@ -382,7 +382,7 @@ ftp_port (struct rbuf *rbuf)
   return FTPOK;
 }
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
 uerr_t
 ftp_epsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port, 
 	  char *typ)
@@ -453,7 +453,7 @@ ftp_pasv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
   uerr_t err;
   unsigned char addr4[4];
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (ip_default_family == AF_INET6) 
     {
       err = ftp_epsv (rbuf, addr, port, "2");	/* try IPv6 with EPSV */
diff --git a/src/ftp.h b/src/ftp.h
index e2c657a6..ad49cc82 100644
--- a/src/ftp.h
+++ b/src/ftp.h
@@ -49,7 +49,7 @@ uerr_t ftp_response PARAMS ((struct rbuf *, char **));
 uerr_t ftp_login PARAMS ((struct rbuf *, const char *, const char *));
 uerr_t ftp_port PARAMS ((struct rbuf *));
 uerr_t ftp_pasv PARAMS ((struct rbuf *, ip_address *, unsigned short *));
-#ifdef INET6
+#ifdef ENABLE_IPV6
 uerr_t ftp_epsv PARAMS ((struct rbuf *, ip_address *, unsigned short *,
 			 char *));
 #endif
diff --git a/src/host.c b/src/host.c
index 5000a202..382707db 100644
--- a/src/host.c
+++ b/src/host.c
@@ -81,7 +81,7 @@ extern int h_errno;
 # endif
 #endif
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
 int     ip_default_family = AF_INET6;
 #else
 int     ip_default_family = AF_INET;
@@ -153,7 +153,7 @@ address_list_set_faulty (struct address_list *al, int index)
     al->faulty = 0;
 }
 
-#ifdef INET6
+#ifdef HAVE_GETADDRINFO
 /**
   * address_list_from_addrinfo
   *
@@ -296,7 +296,7 @@ wget_sockaddr_set_address (wget_sockaddr *sa,
 	}
       return;
     }
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (ip_family == AF_INET6) 
     {
       sa->sin6.sin6_family = ip_family;
@@ -336,7 +336,7 @@ wget_sockaddr_set_port (wget_sockaddr *sa, unsigned short port)
       sa->sin.sin_port = htons (port);
       return;
     }  
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (sa->sa.sa_family == AF_INET6)
     {
       sa->sin6.sin6_port = htons (port);
@@ -366,7 +366,7 @@ wget_sockaddr_get_addr (wget_sockaddr *sa)
 { 
   if (sa->sa.sa_family == AF_INET)
     return &sa->sin.sin_addr;
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (sa->sa.sa_family == AF_INET6)
     return &sa->sin6.sin6_addr;
 #endif
@@ -395,7 +395,7 @@ wget_sockaddr_get_port (const wget_sockaddr *sa)
 {
   if (sa->sa.sa_family == AF_INET)
       return htons (sa->sin.sin_port);
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (sa->sa.sa_family == AF_INET6)
       return htons (sa->sin6.sin6_port);
 #endif
@@ -425,7 +425,7 @@ sockaddr_len ()
 {
   if (ip_default_family == AF_INET) 
     return sizeof (struct sockaddr_in);
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (ip_default_family == AF_INET6) 
     return sizeof (struct sockaddr_in6);
 #endif
@@ -440,7 +440,7 @@ sockaddr_len ()
 void 
 map_ipv4_to_ip (ip4_address *ipv4, ip_address *ip) 
 {
-#ifdef INET6
+#ifdef ENABLE_IPV6
   static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
   memcpy ((char *)ip + 12, ipv4 , 4);
   memcpy ((char *)ip + 0, ipv64, 12);
@@ -458,7 +458,7 @@ map_ipv4_to_ip (ip4_address *ipv4, ip_address *ip)
 int 
 map_ip_to_ipv4 (ip_address *ip, ip4_address *ipv4) 
 {
-#ifdef INET6
+#ifdef ENABLE_IPV6
   static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
   if (0 != memcmp (ip, ipv64, 12))
     return 0;
@@ -473,7 +473,7 @@ map_ip_to_ipv4 (ip_address *ip, ip4_address *ipv4)
 
 /* Versions of gethostbyname and getaddrinfo that support timeout. */
 
-#ifndef INET6
+#ifndef ENABLE_IPV6
 
 struct ghbnwt_context {
   const char *host_name;
@@ -508,7 +508,7 @@ gethostbyname_with_timeout (const char *host_name, int timeout)
   return ctx.hptr;
 }
 
-#else  /* INET6 */
+#else  /* ENABLE_IPV6 */
 
 struct gaiwt_context {
   const char *node;
@@ -548,7 +548,7 @@ getaddrinfo_with_timeout (const char *node, const char *service,
   return ctx.exit_code;
 }
 
-#endif /* INET6 */
+#endif /* ENABLE_IPV6 */
 
 /* Pretty-print ADDR.  When compiled without IPv6, this is the same as
    inet_ntoa.  With IPv6, it either prints an IPv6 address or an IPv4
@@ -557,7 +557,7 @@ getaddrinfo_with_timeout (const char *node, const char *service,
 char *
 pretty_print_address (ip_address *addr)
 {
-#ifdef INET6
+#ifdef ENABLE_IPV6
   ip4_address addr4;
   static char buf[128];
 
@@ -606,7 +606,7 @@ lookup_host (const char *host, int silent)
   /* First, try to check whether the address is already a numeric
      address.  */
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
   if (inet_pton (AF_INET6, host, &addr) > 0)
     return address_list_new_one (&addr);
 #endif
@@ -644,7 +644,7 @@ lookup_host (const char *host, int silent)
 
   /* Host name lookup goes on below. */
 
-#ifdef INET6
+#ifdef HAVE_GETADDRINFO
   {
     struct addrinfo hints, *ai;
     int err;
diff --git a/src/host.h b/src/host.h
index 0fa922a1..3688bd8b 100644
--- a/src/host.h
+++ b/src/host.h
@@ -41,8 +41,6 @@ so, delete this exception statement from your version.  */
 #endif
 #endif
 
-#undef INET6
-
 struct url;
 struct address_list;
 
@@ -52,7 +50,7 @@ struct address_list;
 typedef union {
 	struct sockaddr     sa;   /* Generic but too small */
 	struct sockaddr_in  sin;  /* IPv4 socket address */
-#ifdef INET6
+#ifdef ENABLE_IPV6
 	struct sockaddr_in6 sin6; /* IPv6 socket address */
 #endif
 } wget_sockaddr;
@@ -65,7 +63,7 @@ typedef struct {
    addresses as IPv6 addresses.  IPv4 addresses are dynamically mapped
    to IPv6, i.e. stored in the format ::ffff:<Ipv4>.  */
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
 # define MAX_IP_ADDRESS_SIZE 16
 #else
 # define MAX_IP_ADDRESS_SIZE 4
diff --git a/src/url.c b/src/url.c
index 5435f9bc..eac1cfdd 100644
--- a/src/url.c
+++ b/src/url.c
@@ -660,7 +660,7 @@ static char *parse_errors[] = {
     *(p) = (v);					\
 } while (0)
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
 /* The following two functions were adapted from glibc. */
 
 static int
@@ -863,7 +863,7 @@ url_parse (const char *url, int *error)
 	  return NULL;
 	}
 
-#ifdef INET6
+#ifdef ENABLE_IPV6
       /* Check if the IPv6 address is valid. */
       if (!is_valid_ipv6_address(host_b, host_e))
 	{