     ]
 
     :ok = :gen_tcp.close(state.socket)
+
+    state =
+      if state.receiver do
+        :ok = Receiver.stop(state.receiver)
+        :ok = flush_messages_from_receiver(state)
+        %{state | receiver: nil}
+      else
+        state
+      end
+
     :ok = SharedState.disconnect_clients_and_stop(state.shared_state)
 
     state = %{state | socket: nil, current_backoff: @initial_backoff}

    {:disconnect, {:error, reason}, state}
  end

  defp calc_next_backoff(current_backoff, backoff_max) do
    next_exponential_backoff = round(current_backoff * @backoff_exponent)


      :ok ->
        {:noreply, state}
      {:error, _reason} = error ->
        {:disconnect, error, state}
    end
  end

