private DataStructs.RESULT connect()
        {
            DataStructs.SETTINGS settings = DataStructs.SETTINGS.defaultSettings();
            settings.fake3DMode   = DataStructs.Fake3DMode.DISABLED;
            settings.videoPort    = settingsInstance.videoPort;
            settings.sensorPort   = settingsInstance.sensorPort;
            settings.motionBoost  = settingsInstance.motionBoost;
            settings.clientNoLens = settingsInstance.disableLens;
            settings.ipAddress    = validateIp(settingsInstance.forcedIP);
        #if USE_SYS_DRAW
            settings.convertImage = true;
        #endif
            trinusManager.setSettings(settings);

            DataStructs.RESULT r = trinusManager.connectAsync(3);

            return(r);
        }
        /// <summary>
        /// Taking care of keypresses, connection process and frame preparation
        /// </summary>
        void Update()
        {
            if (trinusCamera == null)
            {
                findCamera();
//				GameObject cam = GameObject.Find ("TrinusCamera");
//				if (cam != null && cam.GetComponent<TrinusCamera> () != null)
//					foundCam = cam.GetComponent<TrinusCamera> ();
//				else {
//					//search for the TrinusCamera script attached to a differently named object
//					TrinusCamera[] cams = GameObject.FindObjectsOfType<TrinusCamera> ();
//					if (cams.Length > 0)
//						foundCam = cams [0];
//					else
//						Debug.LogWarning ("No TrinusCamera found for TrinusManager!");
//				}

//				if (foundCam != null) {
//					decideResolution (PlayerPrefs.GetInt ("resolution", 720), getMonoscopic ());
//					//yawOffset = trinusCamera.defaultViewAngle;
//					switchCamera(foundCam);
//					foundCam.setCameraEnabledDelegate (switchCamera);
//				}
            }

            if (paused)
            {
                return;
            }

            if (Input.GetKeyUp(resetTrackingKey))
            {
                resetTracking();
            }
            if (Input.GetKeyUp(ignoreViewKey))
            {
                disableHeadTracking(!ignoreTracking);
            }

                        #if DEBUG
            if (Input.GetKeyUp(KeyCode.L))
            {
                Debug.Log("Status:" + trinusManager.getStatus());
                Debug.Log("Last result:" + trinusManager.getLastResult());
                Debug.Log("Internal Log:" + trinusManager.getSimpleLog());
            }
                        #endif
            switch (trinusManager.getStatus())
            {
            case DataStructs.STATUS.IDLE:
                                #if DEBUG
                DataStructs.RESULT err = trinusManager.getLastResult();
                if (err.code == DataStructs.RESULT.CODE.ERROR)
                {
                    Debug.Log("Error detected: " + err);
                    Debug.Log(trinusManager.getSimpleLog());
                    setError(err.ToString());
                }
                //Debug.Log("SIMPLE LOG: " + trinusManager.getSimpleLog());
                                #endif

                DataStructs.RESULT r = connect();
                if (r.subCode == DataStructs.RESULT.SUBCODE.SUCCESS)
                {
                    setMessage("msgTrinusWait", settingsInstance.forcedIP == null? "(Auto)" : "(" + settingsInstance.forcedIP + ")");
                }
                else
                {
                    Debug.LogError(r.ToString());
                    Debug.LogError(trinusManager.getSimpleLog());
                    setMessage("msgFailedConn");
                    setError(r.ToString());
                }
                                #if DEBUG
                if (r.subCode == DataStructs.RESULT.SUBCODE.SUCCESS)
                {
                    Debug.Log("Waiting for connection");
                }
                else
                {
                    Debug.Log("Unable to initialize connection " + r);
                }
                                #endif
                break;

            case DataStructs.STATUS.DISCONNECTED:
                DataStructs.RESULT disconnectResult = trinusManager.getLastResult();
                Debug.Log("Disconnected (Reason: " + disconnectResult + ")");
                if (disconnectResult.code == DataStructs.RESULT.CODE.ERROR)                 // && disconnectResult.subCode == DataStructs.RESULT.SUBCODE.ERR_ALREADY_USE) {
                {
                    setMessage("msgFailedConn");
                    setError(disconnectResult.detail);
                }
//				else
                trinusManager.resetDisconnection();
                break;

            case DataStructs.STATUS.CONNECTING:
                setMessage("msgTrinusWait", (string.IsNullOrEmpty(trinusManager.getLastResult().detail)? "" : (settingsInstance.forcedIP != null ? "IP " + settingsInstance.forcedIP : "") +
                                             "Port " + settingsInstance.videoPort + " STEP " + trinusManager.getLastResult().detail));
                if (pauseWhileConnecting && Time.timeScale > 0)
                {
                    Time.timeScale = 0;
                }
                break;

            case DataStructs.STATUS.CONNECTED:
                //setMessage("msgTrinusWait", "CB_" + trinusManager.getLastResult().detail);
                                #if DEBUG
                Debug.Log("Connected, starting streaming " + trinusManager.getLastResult().detail);
                                #endif
                clearMessages();

                lastReviewTime = Time.realtimeSinceStartup;

                DataStructs.DEVICE deviceInfo = trinusManager.getDeviceInfo();

                //targetWidth = Math.Min (deviceInfo.width, Screen.width);
                targetHeight = Math.Min(deviceInfo.height, Screen.height);

                //screen resolution should be equal or lower than device resolution
                //use resolution to balance quality vs performance
                //note: fullscreen mode will have resolution options restricted
                decideResolution(PlayerPrefs.GetInt("resolution", Math.Min(720, targetHeight)), trinusCamera.getMode() == TrinusCamera.CAMERA_MODE.SINGLE);

                trinusManager.startStreaming();

                //resolution info is only sent to device while streaming...
                resolutionChange(Screen.width, Screen.height);

                applyLensParams();

                StartCoroutine(prepareImage());

                if (pauseWhileConnecting)
                {
                    Time.timeScale = 1;
                }

                //trinusCamera.enable ();

                break;

            case DataStructs.STATUS.STREAMING:
                if (settingsInstance.showFps && lastReviewTime + REVIEW_TIME < Time.realtimeSinceStartup)
                {
                    DataStructs.RESULT result = trinusManager.getLastResult();
                    if (result.detail != null)
                    {
                        setInfo(result.detail);
                    }
                    lastReviewTime = Time.realtimeSinceStartup;
                }

                DataStructs.SENSORS sensorData = trinusManager.getSensorData();

                if (!lastTriggerState && sensorData.trigger && triggerPressEvent != null)
                {
                    triggerPressEvent.Invoke();
                }
                if (lastTriggerState && !sensorData.trigger && triggerReleaseEvent != null)
                {
                    triggerReleaseEvent.Invoke();
                }

                lastTriggerState = sensorData.trigger;
                                #if !USE_QUATERNION
                if (!ignoreTracking && trinusCamera != null)
                {
                    //trinusCamera.transform.localEulerAngles = new Vector3 (sensorData.pitch - 90, sensorData.yaw * settingsInstance.yawScale, -sensorData.roll);
                    trinusCamera.transform.localEulerAngles = new Vector3(sensorData.pitch - 90, sensorData.yaw * settingsInstance.yawScale, sensorData.roll);
                }
#else
                cameraRotation.Set(sensorData.quatX, sensorData.quatY, sensorData.quatZ, sensorData.quatW);                 // * Quaternion.Inverse(test);
                if (!ignoreTracking && trinusCamera != null)
                {
                    trinusCamera.transform.localRotation = cameraRotation;
                }
#endif
                acceleration.Set(sensorData.accelX, sensorData.accelY, sensorData.accelZ);

                if (sensorDataDelegate != null)
                {
                    sensorDataDelegate(cameraRotation, acceleration, sensorData.trigger);
                }
                break;
            }
        }