 
   def handle_info({:send, raw_message}, %__MODULE__{socket: socket} = state) do
     Logger.debug(fn -> "Sending message over socket: #{inspect(raw_message)}" end)
-    frame = <<0::5,0::3,byte_size(raw_message)::24>>
-    :gen_tcp.send(socket, frame <> raw_message)
+    :gen_tcp.send(socket, frame_message(raw_message))
     {:noreply, state}
   end
 

defmodule Spell.Transport.RawSocket do
  @moduledoc """
  The `Spell.Transport.RawSocket` module implements a raw socket transport.
  """
  @behaviour Spell.Transport
  require Logger

  # Private Functions

  defp handshake(%__MODULE__{socket: socket, serializer_id: serializer_id, max_length: max_length} = state) do
    :gen_tcp.send(socket, handshake_frame(max_length, serializer_id))
    :gen_tcp.recv(socket, 0)
    |> process_handshake_response(state)
  end

  # WAMP handshake frame format: 0111 1111 LLLL SSSS RRRR RRRR RRRR RRRR
  # LLLL = value is used by the Client to signal the maximum message length of messages it is willing to receive.
    # 0: 2**9 octets
    # 1: 2**10 octets
    # ...
    # 15: 2**24 octets
  # SSSS = used by the Client to request a specific serializer to be used
    # 0: illegal
    # 1: JSON
    # 2: MsgPack
    # 3 - 15: reserved for future serializers
  # RRRR RRRR RRRR RRRR = reserved and MUST be all zeros for now
  defp handshake_frame(max_length, serializer_id) do
    <<127,max_length::4,serializer_id::4,0,0>>
  end

  # WAMP successful handshake response format: 0111 1111 LLLL SSSS RRRR RRRR RRRR RRRR
  # LLLL = limit on the length of messages sent by the Client
  # SSSS = echo the serializer value requested by the Client
  # RRRR RRRR RRRR RRRR = reserved and MUST be all zeros for now
  defp process_handshake_response({:ok, <<127,max_length::4,ser_id::4,0,0>>}, %__MODULE__{serializer_id: ser_id} = state) do
    :inet.setopts(state.socket, active: true)
    :proc_lib.init_ack({:ok, self})
    :gen_server.enter_loop(__MODULE__, [], %{state | router_max_length: max_length})
  end

  # WAMP error handshake response format: 0111 1111 EEEE 0000 RRRR RRRR RRRR RRRR
  # EEEE = encode the error:
    # 0: illegal (must not be used)
    # 1: serializer unsupported
    # 2: maximum message length unacceptable
    # 3: use of reserved bits (unsupported feature)
    # 4: maximum connection count reached
    # 5 - 15: reserved for future errors
  # RRRR RRRR RRRR RRRR = reserved and MUST be all zeros for now
  # TODO: return error explanation instead of code
  defp process_handshake_response({:ok, <<127,error_code::4,0::4,0,0>>}, _state) do
    :proc_lib.init_ack({:error, error_code})
  end
