This array is 16384 elements long. This works but it's just a little too expensive on memory and CPU to be likeable. How about randomly starting at a port in this range and incrementing the port number until an unused port is found?