visage (shad0an) wrote a tool that constantly checks for ping responses from google. I'm not sure if the source is included in the jar file, it might be, or you could just get a decompiler to look at the source. I'm sure he'll give it away too, if you just ask him. So then you can modify it to do what you want. Right now it pings and if there's no response, it pops up a message. You'd want to change it so that it pings, and if there is a response, you do something. It's all written in Java.

http://www.javaspot.net/utils/keepalive.jar