Added error handling for connection loss