If you are working with any communication server and connecting to server using RTMP you can get "appDisconnect" method on server side. Where you can write any logic or can call any function of flex too.
I am using Flex with FluorineFx(.NET) communication server.
Please let me know if require more information for that.