Implemented a heartbeat function to prevent sockets from timing out.