Originally I tried keeping this socket open and forced open. This was problematic and found its just as quick and efficient (and correct?) to open the socket, send command then close the socket.
If it work then this should not be modified ;-)
You can try to leave it open e.g. for 10 minutes and close if no command to send. Something like session timeout.
Nice project :)