 <p>When communicating with TCP or UDP, the socket is distinguished by a 4-tuple:</p> <ul> <li>local address</li> <li>local port</li> <li>remote address</li> <li>remote port</li> </ul> <p>If you leave the local assignment alone, and you are communicating with a connected protocol, the <code>connect</code> call will find an available local port for you. If you are restricted to a particular range of ports, then you'll need to do local address and port binding. However, in that case, since each process is only handling a single connection, I would just assign each process a unique port when it is started, rather than having it try to find an available one.</p> <p>That is, assuming there is a single process responsible for starting all the other processes, that single process would decide which port should be used by the process it is about the start. This is simpler because the decision is made by a single entity, so it can use a simple algorithm, like:</p> <pre><code>for (unsigned short p = minPort; p &lt; maxPort; ++p) { child_pid = startWorker(p); child_pid_map[child_pid] = p; } </code></pre> <p>If the child dies, the starter process can consult the <code>child_pid_map</code> to reuse that port immediately.</p> <p>If using an external agent to assign ports is not possible, the simplest technique is to just attempt a <code>bind</code> call to a random port in the range. If it fails with <code>EADDRINUSE</code>, then incrementally try the next port number in the sequence (wrapping if necessary) until success is achieved, or you have tried all the ports.</p>