added error handling for closed sockets and added test