Find available ports on localhost
Two comments:


The existing code only checks for free ports under 8081.  The full range is 0-65535.
You're only checking the IPv4 definition of 'localhost'.  We live in the future! IPv6 calls localhost ::1.  At first blush, this would seem to not matter - something's either listening or not, right? - but it turns out that firewalls sometimes have different rulesets for IPv4 vs IPv6, so one may get through but the other not.  You didn't specify what this code was for, so it's just a caveat to keep in mind.