Do not use socket.sendall() on non-blocking socket.
Make sure heartbeat is not executed when no longer active.
Stop handling events if socket is closed.