The code does exactly what it is supposed to do if there are VM connections available.
More to the point, what exactly is the point? The best way to tell whether any resource is available is to try to use it, and handle the error or exceptions that result when it isn't. Trying to second-guess how the system decides that is futile and error-prone. If you want to use the network, just use it.