Harden loopback tests

These tests depend on being able to bind port 31337.
Try a range of ports until one binds or we give up trying.