class NettyConnectListener<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectListener.class);

    private final NettyRequestSender requestSender;
    private final NettyResponseFuture<T> future;
    private final ChannelManager channelManager;
    private final ConnectionSemaphore connectionSemaphore;

    public NettyConnectListener(NettyResponseFuture<T> future, NettyRequestSender requestSender, ChannelManager channelManager, ConnectionSemaphore connectionSemaphore) {
        this.future = future;
        this.requestSender = requestSender;
        this.channelManager = channelManager;
        this.connectionSemaphore = connectionSemaphore;
    }

    private boolean futureIsAlreadyCancelled(Channel channel) {
        // If Future is cancelled then we will close the channel silently
        if (future.isCancelled()) {
            Channels.silentlyCloseChannel(channel);
            return true;
        }
        return false;
    }

    private void writeRequest(Channel channel) {
        if (futureIsAlreadyCancelled(channel)) {
            return;
        }

        if (LOGGER.isDebugEnabled()) {
            HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
            LOGGER.debug("Using new Channel '{}' for '{}' to '{}'", channel, httpRequest.method(), httpRequest.uri());
        }

        Channels.setAttribute(channel, future);

        channelManager.registerOpenChannel(channel);
        future.attachChannel(channel, false);
        requestSender.writeRequest(future, channel);
    }

    public void onSuccess(Channel channel, InetSocketAddress remoteAddress) {<FILL_FUNCTION_BODY>}

    public void onFailure(Channel channel, Throwable cause) {

        // beware, channel can be null
        Channels.silentlyCloseChannel(channel);

        boolean canRetry = future.incrementRetryAndCheck();
        LOGGER.debug("Trying to recover from failing to connect channel {} with a retry value of {} ", channel, canRetry);
        if (canRetry//
                && cause != null // FIXME when can we have a null cause?
                && (future.getChannelState() != ChannelState.NEW || StackTraceInspector.recoverOnNettyDisconnectException(cause))) {

            if (requestSender.retry(future)) {
                return;
            }
        }

        LOGGER.debug("Failed to recover from connect exception: {} with channel {}", cause, channel);

        String message = cause.getMessage() != null ? cause.getMessage() : future.getUri().getBaseUrl();
        ConnectException e = new ConnectException(message);
        e.initCause(cause);
        future.abort(e);
    }
}

        if (connectionSemaphore != null) {
            // transfer lock from future to channel
            Object partitionKeyLock = future.takePartitionKeyLock();

            if (partitionKeyLock != null) {
                channel.closeFuture().addListener(future -> connectionSemaphore.releaseChannelLock(partitionKeyLock));
            }
        }

        Channels.setActiveToken(channel);
        TimeoutsHolder timeoutsHolder = future.getTimeoutsHolder();

        if (futureIsAlreadyCancelled(channel)) {
            return;
        }

        Request request = future.getTargetRequest();
        Uri uri = request.getUri();
        timeoutsHolder.setResolvedRemoteAddress(remoteAddress);
        ProxyServer proxyServer = future.getProxyServer();

        // in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request
        if ((proxyServer == null || proxyServer.getProxyType().isSocks()) && uri.isSecured()) {
            SslHandler sslHandler;
            try {
                sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost(), proxyServer != null);
            } catch (Exception sslError) {
                onFailure(channel, sslError);
                return;
            }

            final AsyncHandler<?> asyncHandler = future.getAsyncHandler();

            try {
                asyncHandler.onTlsHandshakeAttempt();
            } catch (Exception e) {
                LOGGER.error("onTlsHandshakeAttempt crashed", e);
                onFailure(channel, e);
                return;
            }

            sslHandler.handshakeFuture().addListener(new SimpleFutureListener<Channel>() {
                @Override
                protected void onSuccess(Channel value) {
                    try {
                        asyncHandler.onTlsHandshakeSuccess(sslHandler.engine().getSession());
                    } catch (Exception e) {
                        LOGGER.error("onTlsHandshakeSuccess crashed", e);
                        NettyConnectListener.this.onFailure(channel, e);
                        return;
                    }
                    writeRequest(channel);
                }

                @Override
                protected void onFailure(Throwable cause) {
                    try {
                        asyncHandler.onTlsHandshakeFailure(cause);
                    } catch (Exception e) {
                        LOGGER.error("onTlsHandshakeFailure crashed", e);
                        NettyConnectListener.this.onFailure(channel, e);
                        return;
                    }
                    NettyConnectListener.this.onFailure(channel, cause);
                }
            });

        } else {
            writeRequest(channel);
        }
    
<no_super_class>