anchor.diff:          #{status := closed} ->
               ok;
           #{status := restarting} ->
  -                catch port_close(PortFD),
  +            stop_gateway(PortFD),
               start_gateway(self(), N, Hostname, Port);
           #{status := failed} ->
  -                catch port_close(PortFD),
  +            stop_gateway(PortFD),
               start_gateway(self(), N, Hostname, Port)
       end.
   
  +%%--------------------------------------------------------------------
  +%% @doc
  +%% Closes port and forces exit of couchbase-sync-gateway executable.
  +%% @end
  +%%--------------------------------------------------------------------
  +-spec stop_gateway(PortFD :: port()) -> ok.
  +stop_gateway(PortFD) ->
  +    {os_pid, OsPid} = erlang:port_info(PortFD, os_pid),
  +        catch port_close(PortFD),
  +    ForcePortCloseCmd = lists:flatten(io_lib:format("kill -9 ~p", [OsPid])),
  +    os:cmd(ForcePortCloseCmd),
  +    ok.
   
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
positive.code:         receive
              {PortFD, {data, {_, Data}}} ->
                  case binary:matches(Data, <<"HTTP:">>) of
                      [] ->
                          ?info("[CouchBase Gateway ~p] ~s", [ID, Data]);
                      _ -> ok
                  end,
                  State;
              {PortFD, closed} ->
                  State#{status => closed};
  
              {'EXIT', PortFD, Reason} ->
                  ?error("CouchBase gateway's port ~p exited with reason: ~p", [State, Reason]),
                  State#{status => failed};
              {port_comm_error, Reason} ->
                  ?error("[CouchBase Gateway ~p] Unable to communicate with port due to: ~p", [ID, Reason]),
                  State#{status => failed};
              restart when CT > MinRestartTime ->
                  ?info("[CouchBase Gateway ~p] Restart request...", [ID]),
                  State#{status => restarting};
              restart ->
                  State;
              {'DOWN', _, process, Parent, Reason} ->
                      catch port_close(PortFD),
                  State#{status => closed};
              stop ->
                      catch port_close(PortFD),
                  State#{status => closed};
              Other ->
                  ?warning("[CouchBase Gateway ~p] ~p", [ID, Other]),
                  State
          after timer:seconds(1) ->
              State
          end,
      case NewState of
          #{status := running} ->
  
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),
  