If you are connect()ing to a remote machine you can simply call connect(), it'll check to see if the socket is unworthy, and will bind() it to an unused local port if necessary.