private void verifyHostname(final SSLSocket sslsock, final String hostname) throws IOException {
        try {
            SSLSession session = sslsock.getSession();
            if (session == null) {
                // In our experience this only happens under IBM 1.4.x when
                // spurious (unrelated) certificates show up in the server'
                // chain.  Hopefully this will unearth the real problem:
                final InputStream in = sslsock.getInputStream();
                in.available();
                // If ssl.getInputStream().available() didn't cause an
                // exception, maybe at least now the session is available?
                session = sslsock.getSession();
                if (session == null) {
                    // If it's still null, probably a startHandshake() will
                    // unearth the real problem.
                    sslsock.startHandshake();
                    session = sslsock.getSession();
                }
            }
            if (session == null) {
                throw new SSLHandshakeException("SSL session not available");
            }
            verifySession(hostname, session);
        } catch (final IOException iox) {
            // close the socket before re-throwing the exception
            Closer.closeQuietly(sslsock);
            throw iox;
        }
    }
Connects the socket to the target host with the given resolved remote address using
{@link Socket#connect(SocketAddress, int)}. This method may be overridden to customize
how precisely {@link Socket#connect(SocketAddress, int)} is handled without impacting
other connection establishment code within {@link #executeHandshake(SSLSocket, String, Object, HttpContext)},
for example.

@param sock the socket to connect.
@param remoteAddress the resolved remote address to connect to.
@param connectTimeout connect timeout.
@param context the actual HTTP context.
@throws IOException if an I/O error occurs
httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java