def start!
      # Steal any remaining data from rack.input
      @buffer = @env[Thin::Request::RACK_INPUT].read + @buffer

      # Remove references to Thin connection objects, freeing memory
      @env.delete Thin::Request::RACK_INPUT
      @env.delete Thin::Request::ASYNC_CALLBACK
      @env.delete Thin::Request::ASYNC_CLOSE

      # Figure out which version we're using
      @version = @env['HTTP_SEC_WEBSOCKET_VERSION']
      @version ||= "hixie-76" if @env.has_key?('HTTP_SEC_WEBSOCKET_KEY1') and @env.has_key?('HTTP_SEC_WEBSOCKET_KEY2')
      @version ||= "hixie-75"

      # Pull out the details we care about
      @origin ||= @env['HTTP_SEC_WEBSOCKET_ORIGIN'] || @env['HTTP_ORIGIN']
      @location ||= "ws#{secure? ? 's' : ''}://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
      @protocol ||= @env['HTTP_SEC_WEBSOCKET_PROTOCOL'] || @env['HTTP_WEBSOCKET_PROTOCOL']

      EM.next_tick { callback :on_start, self rescue error! "Error in start callback" }

      # Queue up the actual handshake
      EM.next_tick method :handshake!

      @state = :started

      # Return self so we can be used as a response
      self
    rescue
      error! "Error starting connection"
    end
def start!
      # Steal any remaining data from rack.input
      @buffer = @env[Thin::Request::RACK_INPUT].read + @buffer

      # Remove references to Thin connection objects, freeing memory
      @env.delete Thin::Request::RACK_INPUT
      @env.delete Thin::Request::ASYNC_CALLBACK
      @env.delete Thin::Request::ASYNC_CLOSE

      # Figure out which version we're using
      @version = @env['HTTP_SEC_WEBSOCKET_VERSION']
      @version ||= "hixie-76" if @env.has_key?('HTTP_SEC_WEBSOCKET_KEY1') and @env.has_key?('HTTP_SEC_WEBSOCKET_KEY2')
      @version ||= "hixie-75"

      # Pull out the details we care about
      @origin ||= @env['HTTP_SEC_WEBSOCKET_ORIGIN'] || @env['HTTP_ORIGIN']
      @location ||= "ws#{secure? ? 's' : ''}://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
      @protocol ||= @env['HTTP_SEC_WEBSOCKET_PROTOCOL'] || @env['HTTP_WEBSOCKET_PROTOCOL']

      EM.next_tick { callback :on_start, self rescue error! "Error in start callback" }

      # Queue up the actual handshake
      EM.next_tick method :handshake!

      @state = :started

      # Return self so we can be used as a response
      self
    rescue
      error! "Error starting connection"
    end
Start the websocket connection