class SporkDebugger::EachRunState

def initialize(connection_request_port)

def initialize(connection_request_port)
  @connection_request_port = connection_request_port
end

def prepare_debugger

def prepare_debugger
  return if @debugger_prepared
  @debugger_prepared = true
  port, cport = start_rdebug_server
  signal_spork_server_to_connect_to_rdebug_server(port)
  wait_for_connection
  puts "\n\n - breakpoint - see your spork server for the debug terminal - \n\n"
end

def signal_spork_server_to_connect_to_rdebug_server(rdebug_server_port)

def signal_spork_server_to_connect_to_rdebug_server(rdebug_server_port)
  socket = TCPSocket.new(SporkDebugger::HOST, @connection_request_port)
  Marshal.dump(rdebug_server_port, socket)
  response = Marshal.load(socket)
  socket.close
end

def start_rdebug_server

def start_rdebug_server
  Debugger.stop if Debugger.started?
  port = find_port(Debugger::PORT)
  cport = find_port(port + 1)
  Debugger.start_remote(SporkDebugger::HOST, [port, cport]) do
    Debugger.run_init_script(StringIO.new)
  end
  Debugger.start
  [port, cport]
end

def wait_for_connection

def wait_for_connection
  while Debugger.handler.interface.nil?; sleep 0.10; end
end