My program needs to generate listerning ports for other processes that are running on the same machine. How to check if port is available?

Thanks!