THe above advice is the way that I would go.. Just to throw my 1/50th of dollar in.. You mention using ping, for just checking connectivity this is fine, but even if you can ping it it doesn't mean the service you are going to use is up. I have had machines that ping fine but have had the ftpd or httpd process die an untimely death.
you need, and like any good network code make sure there is a timeout that is monitored so that if the connection fails the program acknoledges that and moves along.