How can i check via a script if a server is already running on a port?

Im trying to experiment with hosting servers via ports and i need to check if server is running on a port to know if i send .StartClient or .StartHost