@Override
	public Socket createSocket (final Socket plainSocket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
		// we don't need the plainSocket
		if (autoClose) plainSocket.close();
		// create and connect SSL socket, but don't do hostname/certificate verification yet.
		final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
		sslSocketFactory.setTrustManagers(this.trustManager);
		final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
		// Protocols...
		final List<String> protocols = new ArrayList<String>();
		for (final String protocol : sock.getSupportedProtocols()) {
			if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
		}
		sock.setEnabledProtocols(protocols.toArray(new String[0]));
		// Ciphers...
		final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
		ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
		ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites()))); // All all already enabled ones for compatibility.
		sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
		// set up SNI before the handshake.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			sslSocketFactory.setHostname(sock, host);
		}
		else { // This hack seems to work on my 4.0.4 tablet.
			try {
				final java.lang.reflect.Method setHostnameMethod = sock.getClass().getMethod("setHostname", String.class);
				setHostnameMethod.invoke(sock, host);
			}
			catch (final Exception e) {
				LOG.w("SNI not useable: %s", ExcpetionHelper.causeTrace(e));
			}
		}
		// verify hostname and certificate.
		final SSLSession session = sock.getSession();
		if (!HOSTNAME_VERIFIER.verify(host, session)) throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
		LOG.i("Connected %s %s %s.", session.getPeerHost(), session.getProtocol(), session.getCipherSuite());
		return sock;
	}
@Override
	public Socket createSocket (final Socket plainSocket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
	
		if (autoClose) plainSocket.close();
	
		final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
		sslSocketFactory.setTrustManagers(this.trustManager);
		final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
	
		final List<String> protocols = new ArrayList<String>();
		for (final String protocol : sock.getSupportedProtocols()) {
			if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
		}
		sock.setEnabledProtocols(protocols.toArray(new String[0]));
	
		final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
		ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
		ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites())));
		sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
	
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			sslSocketFactory.setHostname(sock, host);
		}
		else {
			try {
				final java.lang.reflect.Method setHostnameMethod = sock.getClass().getMethod("setHostname", String.class);
				setHostnameMethod.invoke(sock, host);
			}
			catch (final Exception e) {
				LOG.w("SNI not useable: %s", ExcpetionHelper.causeTrace(e));
			}
		}
	
		final SSLSession session = sock.getSession();
		if (!HOSTNAME_VERIFIER.verify(host, session)) throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
		LOG.i("Connected %s %s %s.", session.getPeerHost(), session.getProtocol(), session.getCipherSuite());
		return sock;
	}
@override public socket createsocket (final socket plainsocket, final string host, final int port, final boolean autoclose) throws ioexception, unknownhostexception { if (autoclose) plainsocket.close(); final sslcertificatesocketfactory sslsocketfactory = (sslcertificatesocketfactory) sslcertificatesocketfactory.getdefault(0); sslsocketfactory.settrustmanagers(this.trustmanager); final sslsocket sock = (sslsocket) sslsocketfactory.createsocket(inetaddress.getbyname(host), port); final list<string> protocols = new arraylist<string>(); for (final string protocol : sock.getsupportedprotocols()) { if (!protocol.touppercase(locale.english).contains("ssl")) protocols.add(protocol); } sock.setenabledprotocols(protocols.toarray(new string[0])); final hashset<string> ciphers = new hashset<string>(allowed_ciphers); ciphers.retainall(arrays.aslist(sock.getsupportedciphersuites())); ciphers.addall(new hashset<string>(arrays.aslist(sock.getenabledciphersuites()))); sock.setenabledciphersuites(ciphers.toarray(new string[0])); if (build.version.sdk_int >= build.version_codes.jelly_bean_mr1) { sslsocketfactory.sethostname(sock, host); } else { try { final java.lang.reflect.method sethostnamemethod = sock.getclass().getmethod("sethostname", string.class); sethostnamemethod.invoke(sock, host); } catch (final exception e) { log.w("sni not useable: %s", excpetionhelper.causetrace(e)); } } final sslsession session = sock.getsession(); if (!hostname_verifier.verify(host, session)) throw new sslpeerunverifiedexception("cannot verify hostname: " + host); log.i("connected %s %s %s.", session.getpeerhost(), session.getprotocol(), session.getciphersuite()); return sock; }