find free port

how can i find whether the particular port is free among certain port numbers
like 15000 to 30000 before i connect to the server by assigning the port no for the client