// create the instance of the nui sensor
HRESULT KinectSensor::CreateNuiDevice()
{
    assert( m_wsPortID.size() != 0 );

    // check if we can use it
    CComPtr<INuiSensor> pNuiSensor;
    HRESULT hr = NuiCreateSensorById( m_wsPortID.c_str(), &pNuiSensor );
    if( FAILED(hr) )
    {
        return hr;
    }

    // is this one we already have
    if( m_pNuiSensor == pNuiSensor )
    {
        return S_OK;
    }

    assert( nullptr == m_pNuiSensor );

    // if the device is not in use by another process
    if( !IsSensorConflict(pNuiSensor) )
    {
        // start clean
        ResetDevice();

        m_pNuiSensor = pNuiSensor; // auto refcount with CComPtr
    }
    
    // update internal state and return
    return GetNUISensorStatus(true);
}
/// <summary>
/// Update the sensor resource when sensor status changed and the new status is not NuiSensorDisconnected
/// </summary>
void KinectWindowManager::HandleSensorConnected(PCWSTR instanceName, HRESULT hrSensorCallbackStatus)
{
    auto iter = m_sensorMap.find(instanceName);

    // The changed sensor has not been saved
    if (m_sensorMap.end() == iter)
    {
        INuiSensor* pNuiSensor = nullptr;

        if (SUCCEEDED(NuiCreateSensorById(instanceName, &pNuiSensor)))
        {
            iter = AddSensor(instanceName, pNuiSensor);
        }
        else
        {
            return;
        }
    }

    // Show/Hide the Kinect window
    UpdateKinectWindow(hrSensorCallbackStatus, iter->second.NuiSensor, &(iter->second.KinectWindow));
}