I am currently development an application which sends a lot of web requests in a very short space of time continuously(No, it is not a DoS application, to clear any accusations up). I am running multiple threads to speed up the process. However, my client has reported the program crashes. After much searching, I found out that after a port is closed, before it is made available again, it waits for a period according to TIME_WAIT. So my theory is that this program is easily using up the 4000 available ports and the program is crashing.
My question is, how do I find out how many ports are available so that I can avoid this? Another option is of course finding the TIME_WAIT variable out and manually keeping record of how many ports are available, however this is not the best method.