643dd3
diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc
643dd3
index 524eebb..2f09b12 100644
643dd3
--- a/src/clients/FtpGateway.cc
643dd3
+++ b/src/clients/FtpGateway.cc
643dd3
@@ -1834,6 +1834,7 @@ ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback)
643dd3
     }
643dd3
 
643dd3
     ftpState->listenForDataChannel(temp);
643dd3
+    ftpState->data.listenConn = temp;
643dd3
 }
643dd3
 
643dd3
 static void
643dd3
@@ -1869,13 +1870,19 @@ ftpSendPORT(Ftp::Gateway * ftpState)
643dd3
     // pull out the internal IP address bytes to send in PORT command...
643dd3
     // source them from the listen_conn->local
643dd3
 
643dd3
-    struct addrinfo *AI = NULL;
643dd3
+    struct sockaddr_in addr;
643dd3
+    socklen_t addrlen = sizeof(addr);
643dd3
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
643dd3
+    unsigned char port_high = ntohs(addr.sin_port) >> 8;
643dd3
+    unsigned char port_low  = ntohs(addr.sin_port) & 0xff;
643dd3
+
643dd3
+    struct addrinfo *AI = NULL;
643dd3
     ftpState->data.listenConn->local.getAddrInfo(AI, AF_INET);
643dd3
     unsigned char *addrptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_addr;
643dd3
-    unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
643dd3
+    // unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
643dd3
     snprintf(cbuf, CTRL_BUFLEN, "PORT %d,%d,%d,%d,%d,%d\r\n",
643dd3
              addrptr[0], addrptr[1], addrptr[2], addrptr[3],
643dd3
-             portptr[0], portptr[1]);
643dd3
+             port_high, port_low);
643dd3
     ftpState->writeCommand(cbuf);
643dd3
     ftpState->state = Ftp::Client::SENT_PORT;
643dd3
 
643dd3
@@ -1923,14 +1930,27 @@ ftpSendEPRT(Ftp::Gateway * ftpState)
643dd3
         return;
643dd3
     }
643dd3
 
643dd3
-    char buf[MAX_IPSTRLEN];
643dd3
+
643dd3
+    unsigned int port;
643dd3
+    struct sockaddr_storage addr;
643dd3
+    socklen_t addrlen = sizeof(addr);
643dd3
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
643dd3
+    if (addr.ss_family == AF_INET) {
643dd3
+        struct sockaddr_in *addr4 = (struct sockaddr_in*) &addr;
643dd3
+        port = ntohs( addr4->sin_port );
643dd3
+    } else {
643dd3
+        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr;
643dd3
+        port = ntohs( addr6->sin6_port );
643dd3
+    }
643dd3
+
643dd3
+    char buf[MAX_IPSTRLEN];
643dd3
 
643dd3
     /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */
643dd3
     /* Which can be used by EITHER protocol. */
643dd3
-    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%d|\r\n",
643dd3
+    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%u|\r\n",
643dd3
              ( ftpState->data.listenConn->local.isIPv6() ? 2 : 1 ),
643dd3
              ftpState->data.listenConn->local.toStr(buf,MAX_IPSTRLEN),
643dd3
-             ftpState->data.listenConn->local.port() );
643dd3
+             port);
643dd3
 
643dd3
     ftpState->writeCommand(cbuf);
643dd3
     ftpState->state = Ftp::Client::SENT_EPRT;