$network is not suitable for this purpose. If you add a server in Servers list and you're offline, $network returns the name for the network you specified there. Use $server instead: it returns the server name if you're connected, otherwise $null.