How can I reduc the waiting time for ssl handshake?
===

I have the following code from http://sanjaal.com/java/tag/get-public-key-of-ssl-certificate-in-java/ that creates a socket and initiate ssl handshake.

    SSLSocketFactory factory = HttpsURLConnection
    .getDefaultSSLSocketFactory();
     
    System.out.println("Creating a SSL Socket For "+hostname+" on port "+port);
     
    SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);
     
    socket.startHandshake();
    System.out.println("Handshaking Complete");

The problem is that if the handshake could not establish, there is a long waiting time before I see the exception: Connection timed out

How can I set a maximum waiting time ? and what is the suitable minimum amount of time that ensures me that ssl handshake is not going to be successful.