anchor.diff:          receive
               {PortFD, {data, {_, Data}}} ->
                   case binary:matches(Data, <<"HTTP:">>) of
  -                    [] ->
  -                        ?info("[CouchBase Gateway ~p] ~s", [ID, Data]);
  +                    [] -> ?info("[CouchBase Gateway ~p] ~s", [ID, Data]);
                       _ -> ok
                   end,
  -                State;
  +                UpdatedState;
               {PortFD, closed} ->
  -                State#{status => closed};
  -
  +                UpdatedState#{status => closed};
               {'EXIT', PortFD, Reason} ->
  -                ?error("CouchBase gateway's port ~p exited with reason: ~p", [State, Reason]),
  -                State#{status => failed};
  +                ?error("CouchBase gateway's port ~p exited with reason: ~p", [UpdatedState, Reason]),
  +                UpdatedState#{status => failed};
  +            {'EXIT', Parent, Reason} ->
  +                ?info("Parent: ~p down due to: ~p", [Parent, Reason]),
  +                stop_gateway(PortFD),
  +                UpdatedState#{status => closed};
               {port_comm_error, Reason} ->
                   ?error("[CouchBase Gateway ~p] Unable to communicate with port due to: ~p", [ID, Reason]),
  -                State#{status => failed};
  +                UpdatedState#{status => failed};
               restart when CT > MinRestartTime ->
                   ?info("[CouchBase Gateway ~p] Restart request...", [ID]),
  -                State#{status => restarting};
  +                UpdatedState#{status => restarting};
               restart ->
  -                State;
  -            {'DOWN', _, process, Parent, Reason} ->
  -                    catch port_close(PortFD),
  -                State#{status => closed};
  +                UpdatedState;
               stop ->
  -                    catch port_close(PortFD),
  -                State#{status => closed};
  +                stop_gateway(PortFD),
  +                UpdatedState#{status => closed};
               Other ->
                   ?warning("[CouchBase Gateway ~p] ~p", [ID, Other]),
  -                State
  +                UpdatedState
           after timer:seconds(1) ->
  -            State
  +            UpdatedState
           end,
       case NewState of
           #{status := running} ->
  
positive.code:         #{status := closed} ->
              ok;
          #{status := restarting} ->
                  catch port_close(PortFD),
              start_gateway(self(), N, Hostname, Port);
          #{status := failed} ->
                  catch port_close(PortFD),
              start_gateway(self(), N, Hostname, Port)
      end.
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
negative.code: %%--------------------------------------------------------------------
  -spec gateway_loop(State :: #{atom() => term()}) -> no_return().
  gateway_loop(#{port_fd := PortFD, id := {_, N} = ID, db_hostname := Hostname, db_port := Port, gw_port := GWPort,
      start_time := ST, parent := Parent, last_ping_time := LPT} = State) ->
  
      %% Update state
      Gateways = datastore_worker:state_get(db_gateways),
      datastore_worker:state_put(db_gateways, maps:update(N, State, Gateways)),
  
      UpdatedState = case erlang:system_time(milli_seconds) - LPT > timer:seconds(1) of
          true ->
              try
                  true = port_command(PortFD, <<"ping">>, [nosuspend]),
                  {ok, _} = couchbeam:server_info(catch couchbeam:server_connection("localhost", GWPort)),
                  State#{last_ping_time => erlang:system_time(milli_seconds)}
              catch
                  _:{badmap, undefined} ->
                      State; %% State of the worker may not be initialised yet, so there is not way to check if connection is active
                  _:Reason0 ->
                      self() ! {port_comm_error, Reason0},
                      State
              end;
          false ->
              State
      end,
  
      CT = erlang:system_time(milli_seconds),
  