How can I detect that the Flash Player is exiting?

I have written a basic chat client that talks to an XML Socket server. When it starts it connects to the socket and works fine. However, when I close the Flash Player it is still occupying a connection to the server.

I am trying to determine how I can trap the close of the Flash Player and programmatically close the connection to the server before the program exits.

Thanks in advance.