Handle socket disconnection properly