Added error handling to the connect method