internal static bool TryOpenDataConnection(FtpSession session, out DataConnection dataConnection)
{
dataConnection = null;
int tryPort = session.AppServer.FtpServiceProvider.GetRandomPort();
int previousPort = tryPort;
int tryTimes = 0;
IPAddress ipAddress = session.LocalEndPoint.Address;
while (true)
{
var listenSocket = TryListenSocketPort(ipAddress, tryPort);
if (listenSocket != null)
{
dataConnection = new DataConnection(session, listenSocket, tryPort);
return true;
}
tryTimes++;
if (tryTimes > 5)
{
return false;
}
tryPort = session.AppServer.FtpServiceProvider.GetRandomPort();
if (previousPort == tryPort)
{
return false;
}
}
}