error handling and fast exit when connection is lost.