Look into the "connect()" method of the `Socket` class. Basically, you create a blank socket and ask it to connect to a given destination with a specific timeout. This connect() call blocks until a connection was made or timeout was encountered.
final Socket sock = new Socket();
final int timeOut = (int)TimeUnit.SECONDS.toMillis(5); // 5 sec wait period
sock.connect(new InetSocketAddress("host", 8080), timeOut);