I don't believe there is a reliable way to do that. Since internet is essentially a collection of networks with moderate amounts of filtering in between this can fail in multiple ways (in no particular order):
- adapter may have multiple addresses assigned some of which are limited to LAN;
- box may have internal virtual network (virtualbox);
- box may have multiple adapters "connected to the internet" and adapter used to connect to external services will vary from service to service;
- your gateway may allow some services and block others;
- adapter may be connected to LAN that is quite huge.
The only reliable way is to iterate over all addresses assigned to all adapters (maybe discarding loop-back (127.0.0.0/8) and link-local (169.254.0.0/16) addresses on the way) and attempting to connect to external service you are interested in.