3921f5
diff --git a/src/clients/FtpClient.cc b/src/clients/FtpClient.cc
3921f5
index b665bcf..d287e55 100644
3921f5
--- a/src/clients/FtpClient.cc
3921f5
+++ b/src/clients/FtpClient.cc
3921f5
@@ -778,7 +778,8 @@ Ftp::Client::connectDataChannel()
3921f5
 bool
3921f5
 Ftp::Client::openListenSocket()
3921f5
 {
3921f5
-    return false;
3921f5
+    debugs(9, 3, HERE);
3921f5
+	  return false;
3921f5
 }
3921f5
 
3921f5
 /// creates a data channel Comm close callback
3921f5
diff --git a/src/clients/FtpClient.h b/src/clients/FtpClient.h
3921f5
index a76a5a0..218d696 100644
3921f5
--- a/src/clients/FtpClient.h
3921f5
+++ b/src/clients/FtpClient.h
3921f5
@@ -118,7 +118,7 @@ public:
3921f5
     bool sendPort();
3921f5
     bool sendPassive();
3921f5
     void connectDataChannel();
3921f5
-    bool openListenSocket();
3921f5
+    virtual bool openListenSocket();
3921f5
     void switchTimeoutToDataChannel();
3921f5
 
3921f5
     CtrlChannel ctrl; ///< FTP control channel state
3921f5
diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc
3921f5
index 411bce9..31d3e36 100644
3921f5
--- a/src/clients/FtpGateway.cc
3921f5
+++ b/src/clients/FtpGateway.cc
3921f5
@@ -87,6 +87,13 @@ struct GatewayFlags {
3921f5
 class Gateway;
3921f5
 typedef void (StateMethod)(Ftp::Gateway *);
3921f5
 
3921f5
+} // namespace FTP
3921f5
+
3921f5
+static void ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback);
3921f5
+
3921f5
+namespace Ftp
3921f5
+{
3921f5
+
3921f5
 /// FTP Gateway: An FTP client that takes an HTTP request with an ftp:// URI,
3921f5
 /// converts it into one or more FTP commands, and then
3921f5
 /// converts one or more FTP responses into the final HTTP response.
3921f5
@@ -137,7 +144,11 @@ public:
3921f5
 
3921f5
     /// create a data channel acceptor and start listening.
3921f5
     void listenForDataChannel(const Comm::ConnectionPointer &conn;;
3921f5
-
3921f5
+    virtual bool openListenSocket() {
3921f5
+    		debugs(9, 3, HERE);
3921f5
+				ftpOpenListenSocket(this, 0);
3921f5
+        return Comm::IsConnOpen(data.conn);
3921f5
+		}
3921f5
     int checkAuth(const HttpHeader * req_hdr);
3921f5
     void checkUrlpath();
3921f5
     void buildTitleUrl();
3921f5
@@ -1787,6 +1798,7 @@ ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback)
3921f5
     }
3921f5
 
3921f5
     ftpState->listenForDataChannel(temp);
3921f5
+    ftpState->data.listenConn = temp;
3921f5
 }
3921f5
 
3921f5
 static void
3921f5
@@ -1822,13 +1834,19 @@ ftpSendPORT(Ftp::Gateway * ftpState)
3921f5
     // pull out the internal IP address bytes to send in PORT command...
3921f5
     // source them from the listen_conn->local
3921f5
 
3921f5
+    struct sockaddr_in addr;
3921f5
+    socklen_t addrlen = sizeof(addr);
3921f5
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
3921f5
+    unsigned char port_high = ntohs(addr.sin_port) >> 8;
3921f5
+    unsigned char port_low  = ntohs(addr.sin_port) & 0xff;
3921f5
+
3921f5
     struct addrinfo *AI = NULL;
3921f5
     ftpState->data.listenConn->local.getAddrInfo(AI, AF_INET);
3921f5
     unsigned char *addrptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_addr;
3921f5
-    unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
3921f5
+    // unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
3921f5
     snprintf(cbuf, CTRL_BUFLEN, "PORT %d,%d,%d,%d,%d,%d\r\n",
3921f5
              addrptr[0], addrptr[1], addrptr[2], addrptr[3],
3921f5
-             portptr[0], portptr[1]);
3921f5
+             port_high, port_low);
3921f5
     ftpState->writeCommand(cbuf);
3921f5
     ftpState->state = Ftp::Client::SENT_PORT;
3921f5
 
3921f5
@@ -1881,14 +1899,27 @@ ftpSendEPRT(Ftp::Gateway * ftpState)
3921f5
         return;
3921f5
     }
3921f5
 
3921f5
+
3921f5
+    unsigned int port;
3921f5
+    struct sockaddr_storage addr;
3921f5
+    socklen_t addrlen = sizeof(addr);
3921f5
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
3921f5
+    if (addr.ss_family == AF_INET) {
3921f5
+        struct sockaddr_in *addr4 = (struct sockaddr_in*) &addr;
3921f5
+        port = ntohs( addr4->sin_port );
3921f5
+    } else {
3921f5
+        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr;
3921f5
+        port = ntohs( addr6->sin6_port );
3921f5
+    }
3921f5
+
3921f5
     char buf[MAX_IPSTRLEN];
3921f5
 
3921f5
     /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */
3921f5
     /* Which can be used by EITHER protocol. */
3921f5
-    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%d|\r\n",
3921f5
+    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%u|\r\n",
3921f5
              ( ftpState->data.listenConn->local.isIPv6() ? 2 : 1 ),
3921f5
              ftpState->data.listenConn->local.toStr(buf,MAX_IPSTRLEN),
3921f5
-             ftpState->data.listenConn->local.port() );
3921f5
+             port);
3921f5
 
3921f5
     ftpState->writeCommand(cbuf);
3921f5
     ftpState->state = Ftp::Client::SENT_EPRT;
3921f5
@@ -1907,7 +1938,7 @@ ftpReadEPRT(Ftp::Gateway * ftpState)
3921f5
         ftpSendPORT(ftpState);
3921f5
         return;
3921f5
     }
3921f5
-
3921f5
+    ftpState->ctrl.message = NULL;
3921f5
     ftpRestOrList(ftpState);
3921f5
 }
3921f5