Add timeout in socket connection

Try Catch for error handling.
If error, always close socket.
Before closing socket, wait 1 sec.