Indeed, it felt really dirty writing that code :) I didn't trust myself enough to come up with a regex for a valid hostname, so I went for the (potentially) inefficient solution over the (most-likely) insecure one.

If anybody knows of a reliable hostname checking function I can use, I'd be happy to rip this code out. I remember looking for that in the standard library but not finding anything appropriate.