Since you are using sockets in threads, you can probably use the fileno of the socket to monitor when a thread controlling a socket dies. Threaded apps, automatically share the fileno's of the sockets. See
for ideas. The socket-filehandle code there, is GLib based, so you can remove the Gtk2 gui and run it as a command-line threaded app.