class Selenium::WebDriver::SocketPoller

def closed?

def closed?
  with_timeout { !listening? }
end

def conn_completed?(sock)

def conn_completed?(sock)
  sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).int.zero?
end

def connected?

def connected?
  with_timeout { listening? }
end

def current_time

def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(host, port, timeout = 0, interval = 0.25)

def initialize(host, port, timeout = 0, interval = 0.25)
  @host     = host
  @port     = Integer(port)
  @timeout  = Float(timeout)
  @interval = interval
end

def listening?

see https://github.com/jruby/jruby/issues/5709
we use a plain TCPSocket here since JRuby has issues closing socket
def listening?
  TCPSocket.new(@host, @port).close
  true
rescue *NOT_CONNECTED_ERRORS
  false
end

def listening?

def listening?
  addr     = Socket.getaddrinfo(@host, @port, Socket::AF_INET, Socket::SOCK_STREAM)
  sock     = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  sockaddr = Socket.pack_sockaddr_in(@port, addr[0][3].to_s)
  begin
    sock.connect_nonblock sockaddr
  rescue Errno::EINPROGRESS
    retry if socket_writable?(sock) && conn_completed?(sock)
    raise Errno::ECONNREFUSED
  rescue *CONNECTED_ERRORS
    # yay!
  end
  sock.close
  true
rescue *NOT_CONNECTED_ERRORS
  sock&.close
  WebDriver.logger.debug("polling for socket on #{[@host, @port].inspect}", id: :driver_service)
  false
end

def socket_writable?(sock)

def socket_writable?(sock)
  sock.wait_writable(CONNECT_TIMEOUT)
end

def with_timeout

def with_timeout
  max_time = current_time + @timeout
  until current_time > max_time
    return true if yield
    sleep @interval
  end
  false
end