code: def _listen(port: int) -> Optional[socket.socket]:
      try:
          sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          sock.bind(("", port))
          return sock
      except socket.error as e:
          if e.errno == errno.EADDRINUSE:
              return None
          raise
  
file_path: dstack/src/dstack/_internal/core/services/ssh/ports.py
imports: import errno
  import socket
  from typing import Dict, List, Optional
  from dstack._internal.core.errors import DstackError
  from dstack._internal.core.models.configurations import PortMapping
label_desc: Use image node_id 5 for calling the PortsLock obj's underlying member method code with example usage: obj._listen(port) and returns: sock, None