Fix the code, to properly handle the case of the user aborting the connection or the connection being broken.