private Socket createSocket()

in httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java [245:305]


    private Socket createSocket(final HttpHost targetHost) throws IOException {
        final Socket sock;
        if (socketConfig.getSocksProxyAddress() != null) {
            sock = new Socket(new Proxy(Proxy.Type.SOCKS, socketConfig.getSocksProxyAddress()));
        } else {
            sock = new Socket();
        }
        sock.setSoTimeout(socketConfig.getSoTimeout().toMillisecondsIntBound());
        sock.setReuseAddress(socketConfig.isSoReuseAddress());
        sock.setTcpNoDelay(socketConfig.isTcpNoDelay());
        sock.setKeepAlive(socketConfig.isSoKeepAlive());
        if (socketConfig.getRcvBufSize() > 0) {
            sock.setReceiveBufferSize(socketConfig.getRcvBufSize());
        }
        if (socketConfig.getSndBufSize() > 0) {
            sock.setSendBufferSize(socketConfig.getSndBufSize());
        }
        final int linger = socketConfig.getSoLinger().toMillisecondsIntBound();
        if (linger >= 0) {
            sock.setSoLinger(true, linger);
        }

        final InetSocketAddress targetAddress = addressResolver.resolve(targetHost);
        // Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
        // only to this library
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisecondsIntBound());
                return null;
            });
        } catch (final PrivilegedActionException e) {
            Asserts.check(e.getCause() instanceof  IOException,
                    "method contract violation only checked exceptions are wrapped: " + e.getCause());
            // only checked exceptions are wrapped - error and RTExceptions are rethrown by doPrivileged
            throw (IOException) e.getCause();
        }
        if (URIScheme.HTTPS.same(targetHost.getSchemeName())) {
            final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
                    sock, targetHost.getHostName(), targetAddress.getPort(), true);
            if (this.sslSetupHandler != null) {
                final SSLParameters sslParameters = sslSocket.getSSLParameters();
                this.sslSetupHandler.execute(sslParameters);
                sslSocket.setSSLParameters(sslParameters);
            }
            try {
                sslSocket.startHandshake();
                final SSLSession session = sslSocket.getSession();
                if (session == null) {
                    throw new SSLHandshakeException("SSL session not available");
                }
                if (sslSessionVerifier != null) {
                    sslSessionVerifier.verify(targetHost, session);
                }
            } catch (final IOException ex) {
                Closer.closeQuietly(sslSocket);
                throw ex;
            }
            return sslSocket;
        }
        return sock;
    }