3 bugfixes and an improvement

Bugs
* Windows gets a Warning: Invalid CRT parameters detected (fixed in 1b84e4a)
* dead connection does not get detected (fixed in 80bd120)
* heartbeat is not send 5 seconds in advance, but 5 seconds late (fixed in 2ee1d20)

Improvement
e54bf01 removes duplicate code