The timeout shouldn't be cleared at the point above. The later socket.end doesn't guarantee the other end will correctly close down the socket, which means the 'close' event might not happen; so keep the timer running until it does. I realize this shouldn't happen, but I've seen FIN_WAIT_2 sockets hanging out forever when using this with homebridge, which suggests it can.
https://github.com/plasticrake/tplink-smarthome-api/blob/4f684a55f9db919e2ee4274fd60b0c52dcfdb75f/src/network/tcp-socket.ts#L101
The timeout shouldn't be cleared at the point above. The later socket.end doesn't guarantee the other end will correctly close down the socket, which means the 'close' event might not happen; so keep the timer running until it does. I realize this shouldn't happen, but I've seen FIN_WAIT_2 sockets hanging out forever when using this with homebridge, which suggests it can.