Improved error handling for failed connections.