diff --git a/pgadmin/db/pgConn.cpp b/pgadmin/db/pgConn.cpp
index 04853e6..5dbf8bf 100644
--- a/pgadmin/db/pgConn.cpp
+++ b/pgadmin/db/pgConn.cpp
@@ -71,42 +71,52 @@ pgConn::pgConn(const wxString& server, const wxString& database, const wxString&
     utfConnectString = false;
 
     // Check the hostname/ipaddress
-    struct hostent *host;
-    in_addr_t addr;
     conn=0;
     noticeArg=0;
     connStatus = PGCONN_BAD;
     
-#ifdef __WXMSW__
-    struct in_addr ipaddr;
-#else
-    unsigned long ipaddr;
-#endif
-    
 #ifndef __WXMSW__
     if (!(server.IsEmpty() || server.StartsWith(wxT("/"))))
     {
 #endif
-        addr = inet_addr(server.ToAscii());
-        if (addr == INADDR_NONE) // szServer is not an IP address
-        {
-            host = gethostbyname(server.ToAscii());
-            if (host == NULL)
-            {
+        struct addrinfo hints, *servinfo;
+        char ipstr[INET6_ADDRSTRLEN];
+        void *addr;
+        int rv;
+
+        memset(&hints, 0, sizeof hints);
+        hints.ai_family = AF_UNSPEC;
+        hints.ai_socktype = SOCK_STREAM;
+
+        if ((rv = getaddrinfo(server.ToAscii(), (NumToStr((long)port)).ToAscii(), &hints, &servinfo))
+            != 0) {
                 connStatus = PGCONN_DNSERR;
                 wxLogError(__("Could not resolve hostname %s"), server.c_str());
                 return;
-            }
+        }
 
-            memcpy(&(ipaddr),host->h_addr,host->h_length); 
-            hostip = wxString::FromAscii(inet_ntoa(*((struct in_addr*) host->h_addr_list[0])));
-            hostname = server;
+        // get the pointer to the address itself,
+        // different fields in IPv4 and IPv6:
+        if (servinfo->ai_family == AF_INET)
+        {
+            // IPv4
+            struct sockaddr_in *ipv4 = (struct sockaddr_in *)servinfo->ai_addr;
+            addr = &(ipv4->sin_addr);
         }
         else
         {
-            hostip = server;
-            hostname = server;
+            // IPv6
+            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)servinfo->ai_addr;
+            addr = &(ipv6->sin6_addr);
         }
+
+        inet_ntop(servinfo->ai_family, addr, ipstr, sizeof(ipstr));
+        wxString ip(ipstr, wxConvUTF8);
+
+        hostip = ip;
+        hostname = server;
+
+        freeaddrinfo(servinfo);
 #ifndef __WXMSW__
     }
     else
