Is there any reason not to just use the negative port numbers (if you interpret them as signed int so top bit = 1) as the ephemeral ports? The range testing is greatly simplified. Nathan