URScript ending and starting connection through socket

I am using the Polyscope to create a custom program for a UR10e and I have a few questions about sockets.
The program in URScript is acting as a client and I am detecting the disconnection of the communication when I read an empty string and use the socket_close to then close the connection. Is there a better way to detect the loss of the connection and should I be doing something else to properly close it?

Other thing is that sometimes when starting a connection fresh on both ends the URScript program will not connect to the listening server. Is there something that should be done in addition to or for configuration of the socket_open call?