code: def checkPortAvailable(ha):
      
      # Not sure why OS would allow binding to one type and not other.
      # Checking for port available for TCP and UDP.
      sockTypes = (socket.SOCK_DGRAM, socket.SOCK_STREAM)
      for typ in sockTypes:
          sock = socket.socket(socket.AF_INET, typ)
          try:
              sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
              sock.bind(ha)
              if typ == socket.SOCK_STREAM:
                  l_onoff = 1
                  l_linger = 0
                  sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                                  struct.pack('ii', l_onoff, l_linger))
          except OSError as exc:
              if exc.errno in [
                  errno.EADDRINUSE, errno.EADDRNOTAVAIL,
                  WS_SOCKET_BIND_ERROR_ALREADY_IN_USE,
                  WS_SOCKET_BIND_ERROR_NOT_AVAILABLE
              ]:
                  raise PortNotAvailable(ha)
              else:
                  raise exc
          finally:
              sock.close()
docs: Checks whether the given port is available