     :proc_lib.init_ack({:error, reason})
   end
 
+  # TODO: handle ping messages
   defp handle_messages(<<>>, _state), do: :ok
   defp handle_messages(raw_message, state) do
-    <<_res::5,_type::3,length::24,message::binary-size(length)>> <> remaining_messages = raw_message
+    {message, remaining_messages} = extract_message(raw_message)
     :ok = send_to_owner(state.owner, {:message, message})
     handle_messages(remaining_messages, state)
+  end
+
+  # WAMP message frame format: RRRR RTTT LLLL LLLL LLLL LLLL LLLL LLLL
+  # RRRR R = reserved and MUST be all zeros for now
+  # TTT = encode the type of the transport message
+    # 0: regular WAMP message
+    # 1: PING
+    # 2: PONG
+    # 3-7: reserved
+  # LLLL LLLL LLLL LLLL LLLL LLLL = length of the serialized WAMP message
+  defp extract_message(<<_res::5,_type::3,length::24,message::binary-size(length)>> <> remaining_messages) do
+    {message, remaining_messages}
+  end
+
+  defp frame_message(raw_message) do
+    <<0::5,0::3,byte_size(raw_message)::24>> <> raw_message
   end
 
   @spec send_to_owner(pid, any) :: :ok

  # Private Functions

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

  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

  defp process_handshake_response({:ok, <<127,error_code::4,0::4,0,0>>}, _state) do
    :proc_lib.init_ack({:error, error_code})
  end


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

