Unfortunately, the above solution has some drawbacks. The hosts file can override the name to IP lookup and will also work in a disconnected mode. So for example, if I change my hosts file on c:\windows\system32\drivers\etc (note, there is another hosts file on c:\windows\i386) and and entry mapping www.gooogle.com to 188.8.131.52, the above code would return 184.108.40.206 even if my machine is unplugged.
I suggest using the following code to ascertain if your computer is connected to the network.
Of course you will have to made ammends for a proxy server, if any. Note that this is a blocking call and blocks for approx 5 seconds if no network is present.