fixed error handling closing sockets