EchoConnectorHandler handler = new EchoConnectorHandler();
IoSession session = null;
if( !useLocalAddress )
{
ConnectFuture future = connector.connect(
new InetSocketAddress( "localhost", port ),
handler );
future.join();
session = future.getSession();
}
else
{
int clientPort = port;
for( int i = 0; i < 65536; i ++ )
{
clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
try
{
ConnectFuture future = connector.connect(
new InetSocketAddress( "localhost", port ),
new InetSocketAddress( clientPort ),
handler );
future.join();
session = future.getSession();
break;
}
catch( RuntimeIOException e )
{
// Try again until we succeed to bind.