Client: add heartbeat mechanism and handle connection errors gracefully

Edited by Niklas Mertsch