Properly handle socket disconnection