C++ Start/Stop Service on Remote System sometimes return Error 1722 (The RPC server is unavailable)

I have a c++ Application to start and stop services on a remote system. Sometimes when i run the application i get the Errorcode 1722 - The RPC server is unavailable. If i restart the Application everything works again.

The RemoteSystem could be a Windows XP up to Windows 10.

This is the Code that i use to Query the Service Status and the start stop.

    HANDLE hToken;
    BOOL ret = LogonUserA("user", ".", "pwd", LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, &hToken);
    BOOL ret2 = ImpersonateLoggedOnUser(hToken);

    SC_HANDLE _handle;

    _handle = OpenSCManagerA("remoteSystem", NULL, SC_MANAGER_ALL_ACCESS);

    if (!_handle) {
        auto err = GetLastError();
        std::cout << err << std::endl;
        return err;
    }

    SC_HANDLE _serviceHandle;

    _serviceHandle = OpenServiceA(_handle, "Spooler", SERVICE_ALL_ACCESS);

    if (!_serviceHandle) {
        auto err = GetLastError();
        std::cout << err << std::endl;
        return err;
    }

    SERVICE_STATUS ss;

    if (!QueryServiceStatus(_serviceHandle, &ss)) {
        auto err = GetLastError();
        std::cout << err << std::endl;
        return err;
    }

    if (ss.dwCurrentState == SERVICE_RUNNING) {
        SERVICE_STATUS svcStatus;
        if (!ControlService(_serviceHandle, SERVICE_CONTROL_STOP, &svcStatus)) {
            auto err = GetLastError();
            std::cout << err << std::endl;
            return err;
        }
    } else if (ss.dwCurrentState == SERVICE_STOPPED) {
        if (!StartService(_serviceHandle, 0, NULL)) {
            auto err = GetLastError();
            std::cout << err << std::endl;
            return err;
        }
    }

I also used the OpenSCManagerW Methods and pass some Wide Strings with the same result. I could not really reproduce the Problem because it seems to happend completely random