You need to listen to the CLOSE event.
I think..
Code:
myScoket = new XMLSocket();
mySocket.addEventListener(Event.CLOSE, disconnect);
function disconnect(event:Event):void
{
//diconnect it..
}
[Edit]
there is Event.CLOSING, to detect whether a window is about to close and is fired before CLOSE event.
But its AIR only.