Refactor the error handling logic around connect()
Refactor the error handling logic around connect()