// Now try each address
try {
SocketException lastFailure = null;
for (InetAddress address : loopBackAddresses) {
LOGGER.debug("Trying to connect to address {}.", address);
SocketChannel socketChannel;
try {
socketChannel = SocketChannel.open(new InetSocketAddress(address, destinationUri.getPort()));
} catch (SocketException e) {
LOGGER.debug("Cannot connect to address {}, skipping.", address);
lastFailure = e;
continue;
}
LOGGER.debug("Connected to address {}.", address);
URI localAddress = new URI(String.format("tcp://localhost:%d", socketChannel.socket().getLocalPort()));
return new SocketConnection<T>(socketChannel, localAddress, destinationUri, classLoader);
}
throw lastFailure;
} catch (java.net.ConnectException e) {
throw new ConnectException(String.format("Could not connect to server %s. Tried addresses: %s.",