in reply to trap signal?

In the case of an abnormal (before the transaction is complete) close, you can try to catch a SIGPIPE signal. You'll get a SIGPIPE when you try to write (print) to a closed socket, so this is probably your best bet.