Added exception handling when connecting to socket