public ConnectionImpl connectClientToServer(ServerLocation location, boolean forQueue) throws IOException { ConnectionImpl connection = null; boolean initialized = false; try { connection = getConnection(this.ds, this.cancelCriterion); ClientSideHandshake connHandShake = getClientSideHandshake(handshake); connection.connect(endpointManager, location, connHandShake, socketBufferSize, handshakeTimeout, readTimeout, getCommMode(forQueue), this.gatewaySender, this.socketCreator); connection.setHandshake(connHandShake); initialized = true; return connection; } finally { if (!initialized && connection != null) { if (logger.isDebugEnabled()) { logger.debug("Destroying failed connection to {}", location); } destroyConnection(connection); } } }