Given:
- Internet and system requirements to work 24/7, every day network breaks from the provider, etc.
- the server must regularly check the connection in order to quickly detect a break and remove the client from the list of available
- the client must regularly check the connection in order to quickly detect a break and begin attempts to reconnect to the server
Without any hesitation, I solved the problem head-on - I made two separate “circuits” of heartbeats, one for the client, the other for the server. If you throw away the rest of the logic, the scheme is as follows (top - chronology, bottom - client algorithm and server algorithm): 
But what happens? Every N seconds (sleep is not shown in the picture, but it is there and there), four packets are sent over the channel — a bit too much. And the algorithm turned out quite confusing.
Is it possible to somehow simplify the scheme, limiting only two packages?
MSG_OOBflag insend()andrecv()calls, more details in man 7 tcp ) , which is probably more convenient for the implementation of heartbeat, since in this case the heartbeat data will not interfere with the main exchange - avp