Added timeout and error handling for errors when connecting