def perform(action, payload = {})
      payload.merge!(
        cmd: action,
        version: DeathByCaptcha::API_VERSION,
        username: self.username,
        password: self.password
      )

      socket = create_socket()
      socket.puts(payload.to_json)
      response = socket.read()
      socket.close()

      begin
        response = JSON.parse(response)
      rescue
        raise DeathByCaptcha::APIResponseError.new("invalid JSON: #{response}")
      end

      if !(error = response['error'].to_s).empty?
        case error
        when 'not-logged-in', 'invalid-credentials', 'banned', 'insufficient-funds'
          raise DeathByCaptcha::APIForbidden
        when 'invalid-captcha'
          raise DeathByCaptcha::APIBadRequest
        when 'service-overload'
          raise DeathByCaptcha::APIServiceUnavailable
        else
          raise DeathByCaptcha::APIResponseError.new(error)
        end
      end

      response
    end
def perform(action, payload = {})
      payload.merge!(
        cmd: action,
        version: DeathByCaptcha::API_VERSION,
        username: self.username,
        password: self.password
      )

      socket = create_socket()
      socket.puts(payload.to_json)
      response = socket.read()
      socket.close()

      begin
        response = JSON.parse(response)
      rescue
        raise DeathByCaptcha::APIResponseError.new("invalid JSON: #{response}")
      end

      if !(error = response['error'].to_s).empty?
        case error
        when 'not-logged-in', 'invalid-credentials', 'banned', 'insufficient-funds'
          raise DeathByCaptcha::APIForbidden
        when 'invalid-captcha'
          raise DeathByCaptcha::APIBadRequest
        when 'service-overload'
          raise DeathByCaptcha::APIServiceUnavailable
        else
          raise DeathByCaptcha::APIResponseError.new(error)
        end
      end

      response
    end
Perform a Socket communication with the DeathByCaptcha API.

 @param [String] action  API method name.
 @param [Hash]   payload Data to be exchanged in the communication.

 @return [Hash] Response from the DeathByCaptcha API.