Refactored client to handle disconnection in a graceful manner