diff --git a/lib/rfc959.c b/lib/rfc959.c
index 7eddf28..d55dd10 100644
--- a/lib/rfc959.c
+++ b/lib/rfc959.c
@@ -863,7 +863,8 @@ static int
 rfc959_ipv6_data_connection_new (gftp_request * request)
 {
   struct sockaddr_in6 data_addr;
-  char *pos, buf[64], *command;
+  socklen_t data_addr_len;
+  char *pos, addrstr[INET6_ADDRSTRLEN], *command;
   intptr_t passive_transfer;
   rfc959_parms * parms;
   unsigned int port;
@@ -897,7 +898,8 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
       return (GFTP_EFATAL);
     }
 
-  memset (&data_addr, 0, sizeof (data_addr));
+  data_addr_len = sizeof(data_addr);
+  memset (&data_addr, 0, data_addr_len);
   data_addr.sin6_family = AF_INET6;
 
   gftp_lookup_request_option (request, "passive_transfer", &passive_transfer);
@@ -935,7 +937,7 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
           return (GFTP_EFATAL);
         }
 
-      memcpy (&data_addr, request->remote_addr, request->remote_addr_len);
+      memcpy (&data_addr.sin6_addr, request->remote_addr, request->remote_addr_len);
       data_addr.sin6_port = htons (port);
 
       if (connect (parms->data_connection, (struct sockaddr *) &data_addr, 
@@ -950,11 +952,20 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
     }
   else
     {
-      memcpy (&data_addr, request->remote_addr, request->remote_addr_len);
-      data_addr.sin6_port = 0;
 
+      if (getsockname (request->datafd, (struct sockaddr *) &data_addr, 
+                       &data_addr_len) == -1)
+        {
+          request->logging_function (gftp_logging_error, request,
+           _("Cannot get socket name: %s\n"),
+             g_strerror (errno));
+          gftp_disconnect (request);
+          return (GFTP_ERETRYABLE);
+        }
+
+      data_addr.sin6_port = 0;
       if (bind (parms->data_connection, (struct sockaddr *) &data_addr, 
-                request->remote_addr_len) == -1)
+                data_addr_len) == -1)
 	{
 	  request->logging_function (gftp_logging_error, request,
 				     _("Cannot bind a port: %s\n"),
@@ -964,7 +975,7 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
 	}
 
       if (getsockname (parms->data_connection, (struct sockaddr *) &data_addr, 
-                       &request->remote_addr_len) == -1)
+                       &data_addr_len) == -1)
         {
           request->logging_function (gftp_logging_error, request,
 				     _("Cannot get socket name: %s\n"),
@@ -983,7 +994,7 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
 	  return (GFTP_ERETRYABLE);
 	}
 
-      if (inet_ntop (AF_INET6, &data_addr.sin6_addr, buf, sizeof (buf)) == NULL)
+      if (inet_ntop (AF_INET6, &data_addr.sin6_addr, addrstr, INET6_ADDRSTRLEN) == NULL)
         {
           request->logging_function (gftp_logging_error, request,
 				     _("Cannot get address of local socket: %s\n"),
@@ -992,7 +1003,7 @@ rfc959_ipv6_data_connection_new (gftp_request * request)
 	  return (GFTP_ERETRYABLE);
         }
 
-      command = g_strdup_printf ("EPRT |2|%s|%d|\n", buf,
+      command = g_strdup_printf ("EPRT |2|%s|%d|\n", addrstr,
                                  ntohs (data_addr.sin6_port));
 
       resp = rfc959_send_command (request, command, -1, 1, 1);
diff --git a/lib/socket-connect-getaddrinfo.c b/lib/socket-connect-getaddrinfo.c
index 4bdbd4c..da21df3 100644
--- a/lib/socket-connect-getaddrinfo.c
+++ b/lib/socket-connect-getaddrinfo.c
@@ -26,6 +26,7 @@ static int
 get_port (struct addrinfo *addr)
 {
   struct sockaddr_in * saddr;
+  struct sockaddr_in6 * saddr6;
   int port;
 
   if (addr->ai_family == AF_INET)
@@ -33,6 +34,11 @@ get_port (struct addrinfo *addr)
       saddr = (struct sockaddr_in *) addr->ai_addr;
       port = ntohs (saddr->sin_port);
     }
+  else if (addr->ai_family == AF_INET6)
+  {
+      saddr6 = (struct sockaddr_in6*) addr->ai_addr;
+      port = ntohs(saddr6->sin6_port);
+  }
   else
     port = 0;
 
@@ -145,8 +151,15 @@ gftp_connect_server_with_getaddrinfo (gftp_request * request, char *service,
 
   request->remote_addr_len = current_hostp->ai_addrlen;
   request->remote_addr = g_malloc0 (request->remote_addr_len);
-  memcpy (request->remote_addr, &((struct sockaddr_in *) current_hostp->ai_addr)->sin_addr,
-          request->remote_addr_len);
+
+  if (current_hostp->ai_family == AF_INET) {
+      memcpy (request->remote_addr, &((struct sockaddr_in *) current_hostp->ai_addr)->sin_addr,
+              request->remote_addr_len);
+  }
+  else if (current_hostp->ai_family == AF_INET6) {
+      memcpy (request->remote_addr, &((struct sockaddr_in6 *) current_hostp->ai_addr)->sin6_addr,
+              request->remote_addr_len);
+  }
 
   request->logging_function (gftp_logging_misc, request,
                              _("Connected to %s:%d\n"), res[0].ai_canonname,
