Writing a port scanner from scratch in Python