public string SendJSCommand( string command )
		{
			if ( !_connection.Connect() )
			{
				return "Error connecting to Photoshop.";
			}

			var stream = _connection.Stream;

			string password = EditorPrefs.GetString( "AmplifyColor.NetworkPassword", "password" );
			_encryptDecrypt = new EncryptDecrypt( password );

			try
			{
				/* WRITE */
				byte[] strBytes = _utf8Encoding.GetBytes( command + "\n" );
				byte[] allBytes;

				using ( var memoryStream = new MemoryStream() )
				{
					BitConverter.GetBytes( ProtocolVersion );
					memoryStream.Write( BitConverter.GetBytes( ProtocolVersion ).Reverse().ToArray(), 0, 4 );
					_transactionId++;
					memoryStream.Write( BitConverter.GetBytes( _transactionId ).Reverse().ToArray(), 0, 4 );
					memoryStream.Write( BitConverter.GetBytes( JavascriptType ).Reverse().ToArray(), 0, 4 );
					memoryStream.Write( strBytes, 0, strBytes.Length );

					allBytes = memoryStream.GetBuffer().Take( ProtocolLength + strBytes.Length ).ToArray();
				}

				var encryptedBytes = _encryptDecrypt.encrypt( allBytes );

				int messageLength = CommLength + encryptedBytes.Length;

				stream.Write( BitConverter.GetBytes( messageLength ).Reverse().ToArray(), 0, 4 );
				stream.Write( BitConverter.GetBytes( NoCommError ).Reverse().ToArray(), 0, 4 );
				stream.Write( encryptedBytes, 0, encryptedBytes.Length );

				/* READ */

				var buffer = new byte[ 4 ];

				stream.Read( buffer, 0, 4 );
				buffer = buffer.Reverse().ToArray();
				int inLength = BitConverter.ToInt32( buffer, 0 );


				stream.Read( buffer, 0, 4 );
				buffer = buffer.Reverse().ToArray();
				int inComStatus = BitConverter.ToInt32( buffer, 0 );

				if ( inComStatus != 0 )
				{
					stream.Read( buffer, 0, 4 );
					buffer = buffer.Reverse().ToArray();
					//int inVersion = BitConverter.ToInt32(buffer, 0);

					stream.Read( buffer, 0, 4 );
					buffer = buffer.Reverse().ToArray();
					//int inTransaction = BitConverter.ToInt32(buffer, 0);

					stream.Read( buffer, 0, 4 );
					buffer = buffer.Reverse().ToArray();
					int inType = BitConverter.ToInt32( buffer, 0 );

					if ( inType == JavascriptType || inType == ErrorstringType )
					{
						inLength -= ProtocolLength;
						var bytemessage = new byte[ inLength + 1 ];
						int rr = stream.Read( bytemessage, 0, inLength );

						if ( rr > 0 )
						{
							var encoder = new UTF8Encoding();
							var message = encoder.GetString( bytemessage );
							Debug.LogWarning( "[AmplifyColor] Uncoded Message: " + message + ". Please check your password." );
							return message.Trim( new[] { '\0' } );
						}
					}
					else
					{
						return "Message types: " + inType;
					}
				}
				else
				{
					inLength = inLength - 4;

					var messageBytes = new byte[ inLength ];
					stream.Read( messageBytes, 0, inLength );

					IEnumerable<byte> decryptedBytes = _encryptDecrypt.decrypt( messageBytes );

					byte[] tempbytes;
					//tempbytes = decryptedBytes.Take(4).Reverse().ToArray();
					decryptedBytes = decryptedBytes.Skip( 4 );
					//int messageVersion = BitConverter.ToInt32(tempbytes, 0);

					//tempbytes = decryptedBytes.Take(4).Reverse().ToArray();
					decryptedBytes = decryptedBytes.Skip( 4 );
					//int messageId = BitConverter.ToInt32(tempbytes, 0);

					tempbytes = decryptedBytes.Take( 4 ).Reverse().ToArray();
					decryptedBytes = decryptedBytes.Skip( 4 );
					int messageType = BitConverter.ToInt32( tempbytes, 0 );

					if ( messageType == JavascriptType || messageType == ErrorstringType )
					{
						var encoder = new UTF8Encoding();
						var message = encoder.GetString( decryptedBytes.ToArray() );
						return message.Trim( new[] { '\0' } );
					}

					return "Message types: " + messageType;
				}
			}
			catch ( Exception e )
			{
				return "Exception: " + e.Message;
			}

			return string.Empty;
		}
        public string SendJSCommand(string command)
        {
            if (!_connection.Connect())
            {
                return("Error connecting to Photoshop.");
            }

            var stream = _connection.Stream;

            string password = EditorPrefs.GetString("AmplifyColor.NetworkPassword", "password");

            _encryptDecrypt = new EncryptDecrypt(password);

            try
            {
                /* WRITE */
                byte[] strBytes = _utf8Encoding.GetBytes(command + "\n");
                byte[] allBytes;

                using (var memoryStream = new MemoryStream())
                {
                    BitConverter.GetBytes(ProtocolVersion);
                    memoryStream.Write(BitConverter.GetBytes(ProtocolVersion).Reverse().ToArray(), 0, 4);
                    _transactionId++;
                    memoryStream.Write(BitConverter.GetBytes(_transactionId).Reverse().ToArray(), 0, 4);
                    memoryStream.Write(BitConverter.GetBytes(JavascriptType).Reverse().ToArray(), 0, 4);
                    memoryStream.Write(strBytes, 0, strBytes.Length);

                    allBytes = memoryStream.GetBuffer().Take(ProtocolLength + strBytes.Length).ToArray();
                }

                var encryptedBytes = _encryptDecrypt.encrypt(allBytes);

                int messageLength = CommLength + encryptedBytes.Length;

                stream.Write(BitConverter.GetBytes(messageLength).Reverse().ToArray(), 0, 4);
                stream.Write(BitConverter.GetBytes(NoCommError).Reverse().ToArray(), 0, 4);
                stream.Write(encryptedBytes, 0, encryptedBytes.Length);

                /* READ */

                var buffer = new byte[4];

                stream.Read(buffer, 0, 4);
                buffer = buffer.Reverse().ToArray();
                int inLength = BitConverter.ToInt32(buffer, 0);


                stream.Read(buffer, 0, 4);
                buffer = buffer.Reverse().ToArray();
                int inComStatus = BitConverter.ToInt32(buffer, 0);

                if (inComStatus != 0)
                {
                    stream.Read(buffer, 0, 4);
                    buffer = buffer.Reverse().ToArray();
                    //int inVersion = BitConverter.ToInt32(buffer, 0);

                    stream.Read(buffer, 0, 4);
                    buffer = buffer.Reverse().ToArray();
                    //int inTransaction = BitConverter.ToInt32(buffer, 0);

                    stream.Read(buffer, 0, 4);
                    buffer = buffer.Reverse().ToArray();
                    int inType = BitConverter.ToInt32(buffer, 0);

                    if (inType == JavascriptType || inType == ErrorstringType)
                    {
                        inLength -= ProtocolLength;
                        var bytemessage = new byte[inLength + 1];
                        int rr          = stream.Read(bytemessage, 0, inLength);

                        if (rr > 0)
                        {
                            var encoder = new UTF8Encoding();
                            var message = encoder.GetString(bytemessage);
                            Debug.LogWarning("[AmplifyColor] Uncoded Message: " + message + ". Please check your password.");
                            return(message.Trim(new[] { '\0' }));
                        }
                    }
                    else
                    {
                        return("Message types: " + inType);
                    }
                }
                else
                {
                    inLength = inLength - 4;

                    var messageBytes = new byte[inLength];
                    stream.Read(messageBytes, 0, inLength);

                    IEnumerable <byte> decryptedBytes = _encryptDecrypt.decrypt(messageBytes);

                    byte[] tempbytes;
                    //tempbytes = decryptedBytes.Take(4).Reverse().ToArray();
                    decryptedBytes = decryptedBytes.Skip(4);
                    //int messageVersion = BitConverter.ToInt32(tempbytes, 0);

                    //tempbytes = decryptedBytes.Take(4).Reverse().ToArray();
                    decryptedBytes = decryptedBytes.Skip(4);
                    //int messageId = BitConverter.ToInt32(tempbytes, 0);

                    tempbytes      = decryptedBytes.Take(4).Reverse().ToArray();
                    decryptedBytes = decryptedBytes.Skip(4);
                    int messageType = BitConverter.ToInt32(tempbytes, 0);

                    if (messageType == JavascriptType || messageType == ErrorstringType)
                    {
                        var encoder = new UTF8Encoding();
                        var message = encoder.GetString(decryptedBytes.ToArray());
                        return(message.Trim(new[] { '\0' }));
                    }

                    return("Message types: " + messageType);
                }
            }
            catch (Exception e)
            {
                return("Exception: " + e.Message);
            }

            return(string.Empty);
        }
		public string ReceiveImage( string documentName, out ImageResult imageData )
		{
			imageData = null;

			if ( !_connection.Connect() )
			{
				return "Error connecting to Photoshop.";
			}

			var stream = _connection.Stream;

			string password = EditorPrefs.GetString( "AmplifyColor.NetworkPassword", "password" );
			_encryptDecrypt = new EncryptDecrypt( password );

			var commandBuilder = new StringBuilder();

			if ( !string.IsNullOrEmpty( documentName ) )
			{
				commandBuilder.Append( "app.activeDocument = app.documents.getByName('" + documentName + "');\n" );
			}

			commandBuilder.Append( "var idNS = stringIDToTypeID('sendDocumentThumbnailToNetworkClient');\n" );
			commandBuilder.Append( "var desc1 = new ActionDescriptor();\n" );
			commandBuilder.Append( "desc1.putInteger( stringIDToTypeID('width'), app.activeDocument.width );\n" );
			commandBuilder.Append( "desc1.putInteger( stringIDToTypeID('height'), app.activeDocument.height );\n" );
			commandBuilder.Append( "desc1.putInteger( stringIDToTypeID('format'), 2 );\n" );
			commandBuilder.Append( "executeAction( idNS, desc1, DialogModes.NO );" );
			string command = commandBuilder.ToString();

			try
			{
				/* WRITE */
				byte[] strBytes = _utf8Encoding.GetBytes( command + "\n" );
				byte[] allBytes;

				using ( var memoryStream = new MemoryStream() )
				{
					BitConverter.GetBytes( ProtocolVersion );
					memoryStream.Write( BitConverter.GetBytes( ProtocolVersion ).Reverse().ToArray(), 0, 4 );
					_transactionId++;
					memoryStream.Write( BitConverter.GetBytes( _transactionId ).Reverse().ToArray(), 0, 4 );
					memoryStream.Write( BitConverter.GetBytes( JavascriptType ).Reverse().ToArray(), 0, 4 );
					memoryStream.Write( strBytes, 0, strBytes.Length );

					allBytes = memoryStream.GetBuffer().Take( ProtocolLength + strBytes.Length ).ToArray();
				}

				var encryptedBytes = _encryptDecrypt.encrypt( allBytes );

				int messageLength = CommLength + encryptedBytes.Length;

				stream.Write( BitConverter.GetBytes( messageLength ).Reverse().ToArray(), 0, 4 );
				stream.Write( BitConverter.GetBytes( NoCommError ).Reverse().ToArray(), 0, 4 );
				stream.Write( encryptedBytes, 0, encryptedBytes.Length );

				/* READ */
				DateTime start = DateTime.Now;
				int messageType = 0;
				while ( messageType != ImageType && ( DateTime.Now - start ).TotalMilliseconds < 1000 )
				{
					if ( !stream.DataAvailable )
						continue;

					var buffer = new byte[ 4 ];

					stream.Read( buffer, 0, 4 );
					buffer = buffer.Reverse().ToArray();
					int inLength = BitConverter.ToInt32( buffer, 0 );


					stream.Read( buffer, 0, 4 );
					buffer = buffer.Reverse().ToArray();
					int inComStatus = BitConverter.ToInt32( buffer, 0 );

					if ( inComStatus != 0 )
					{
						stream.Read( buffer, 0, 4 );
						buffer = buffer.Reverse().ToArray();
						//int inVersion = BitConverter.ToInt32(buffer, 0);

						stream.Read( buffer, 0, 4 );
						buffer = buffer.Reverse().ToArray();
						//int inTransaction = BitConverter.ToInt32(buffer, 0);

						stream.Read( buffer, 0, 4 );
						buffer = buffer.Reverse().ToArray();
						messageType = BitConverter.ToInt32( buffer, 0 );

						if ( messageType == JavascriptType || messageType == ErrorstringType )
						{
							inLength -= ProtocolLength;
							var bytemessage = new byte[ inLength + 1 ];
							int rr = stream.Read( bytemessage, 0, inLength );

							if ( rr > 0 )
							{
								var encoder = new UTF8Encoding();
								var message = encoder.GetString( bytemessage );
								return message.Trim( new[] { '\0' } );
							}
						}
						else
						{
							return "Message types: " + messageType;
						}
					}
					else
					{
						inLength = inLength - 4;

						var messageBytes = new List<byte>();
						var messagebuffer = new byte[ 1000 ];
						int totalread = 0;

						while ( totalread < inLength || stream.DataAvailable )
						{
							int bytesread = stream.Read( messagebuffer, 0, 1000 );
							totalread += bytesread;
							messageBytes.AddRange( messagebuffer.Take( bytesread ) );
						}

						IEnumerable<byte> decryptedBytes = _encryptDecrypt.decrypt( messageBytes.ToArray() );

						decryptedBytes = decryptedBytes.Skip( 4 );
						decryptedBytes = decryptedBytes.Skip( 4 );

						byte[] tempbytes = decryptedBytes.Take( 4 ).Reverse().ToArray();
						decryptedBytes = decryptedBytes.Skip( 4 );

						messageType = BitConverter.ToInt32( tempbytes, 0 );

						if ( messageType == ImageType )
							imageData = ImageResult.FromPhotoshopResult( decryptedBytes.ToArray() );
						else if ( messageType == ErrorstringType )
							return Encoding.UTF8.GetString( decryptedBytes.ToArray() );
					}
				}

				//return "Message types: " + messageType;
				return "Image Received";
			}
			catch ( Exception e )
			{
				return e.Message;
			}
		}
        public string ReceiveImage(string documentName, out ImageResult imageData)
        {
            imageData = null;

            if (!_connection.Connect())
            {
                return("Error connecting to Photoshop.");
            }

            var stream = _connection.Stream;

            string password = EditorPrefs.GetString("AmplifyColor.NetworkPassword", "password");

            _encryptDecrypt = new EncryptDecrypt(password);

            var commandBuilder = new StringBuilder();

            if (!string.IsNullOrEmpty(documentName))
            {
                commandBuilder.Append("app.activeDocument = app.documents.getByName('" + documentName + "');\n");
            }

            commandBuilder.Append("var idNS = stringIDToTypeID('sendDocumentThumbnailToNetworkClient');\n");
            commandBuilder.Append("var desc1 = new ActionDescriptor();\n");
            commandBuilder.Append("desc1.putInteger( stringIDToTypeID('width'), app.activeDocument.width );\n");
            commandBuilder.Append("desc1.putInteger( stringIDToTypeID('height'), app.activeDocument.height );\n");
            commandBuilder.Append("desc1.putInteger( stringIDToTypeID('format'), 2 );\n");
            commandBuilder.Append("executeAction( idNS, desc1, DialogModes.NO );");
            string command = commandBuilder.ToString();

            try
            {
                /* WRITE */
                byte[] strBytes = _utf8Encoding.GetBytes(command + "\n");
                byte[] allBytes;

                using (var memoryStream = new MemoryStream())
                {
                    BitConverter.GetBytes(ProtocolVersion);
                    memoryStream.Write(BitConverter.GetBytes(ProtocolVersion).Reverse().ToArray(), 0, 4);
                    _transactionId++;
                    memoryStream.Write(BitConverter.GetBytes(_transactionId).Reverse().ToArray(), 0, 4);
                    memoryStream.Write(BitConverter.GetBytes(JavascriptType).Reverse().ToArray(), 0, 4);
                    memoryStream.Write(strBytes, 0, strBytes.Length);

                    allBytes = memoryStream.GetBuffer().Take(ProtocolLength + strBytes.Length).ToArray();
                }

                var encryptedBytes = _encryptDecrypt.encrypt(allBytes);

                int messageLength = CommLength + encryptedBytes.Length;

                stream.Write(BitConverter.GetBytes(messageLength).Reverse().ToArray(), 0, 4);
                stream.Write(BitConverter.GetBytes(NoCommError).Reverse().ToArray(), 0, 4);
                stream.Write(encryptedBytes, 0, encryptedBytes.Length);

                /* READ */
                DateTime start       = DateTime.Now;
                int      messageType = 0;
                while (messageType != ImageType && (DateTime.Now - start).TotalMilliseconds < 1000)
                {
                    if (!stream.DataAvailable)
                    {
                        continue;
                    }

                    var buffer = new byte[4];

                    stream.Read(buffer, 0, 4);
                    buffer = buffer.Reverse().ToArray();
                    int inLength = BitConverter.ToInt32(buffer, 0);


                    stream.Read(buffer, 0, 4);
                    buffer = buffer.Reverse().ToArray();
                    int inComStatus = BitConverter.ToInt32(buffer, 0);

                    if (inComStatus != 0)
                    {
                        stream.Read(buffer, 0, 4);
                        buffer = buffer.Reverse().ToArray();
                        //int inVersion = BitConverter.ToInt32(buffer, 0);

                        stream.Read(buffer, 0, 4);
                        buffer = buffer.Reverse().ToArray();
                        //int inTransaction = BitConverter.ToInt32(buffer, 0);

                        stream.Read(buffer, 0, 4);
                        buffer      = buffer.Reverse().ToArray();
                        messageType = BitConverter.ToInt32(buffer, 0);

                        if (messageType == JavascriptType || messageType == ErrorstringType)
                        {
                            inLength -= ProtocolLength;
                            var bytemessage = new byte[inLength + 1];
                            int rr          = stream.Read(bytemessage, 0, inLength);

                            if (rr > 0)
                            {
                                var encoder = new UTF8Encoding();
                                var message = encoder.GetString(bytemessage);
                                return(message.Trim(new[] { '\0' }));
                            }
                        }
                        else
                        {
                            return("Message types: " + messageType);
                        }
                    }
                    else
                    {
                        inLength = inLength - 4;

                        var messageBytes  = new List <byte>();
                        var messagebuffer = new byte[1000];
                        int totalread     = 0;

                        while (totalread < inLength || stream.DataAvailable)
                        {
                            int bytesread = stream.Read(messagebuffer, 0, 1000);
                            totalread += bytesread;
                            messageBytes.AddRange(messagebuffer.Take(bytesread));
                        }

                        IEnumerable <byte> decryptedBytes = _encryptDecrypt.decrypt(messageBytes.ToArray());

                        decryptedBytes = decryptedBytes.Skip(4);
                        decryptedBytes = decryptedBytes.Skip(4);

                        byte[] tempbytes = decryptedBytes.Take(4).Reverse().ToArray();
                        decryptedBytes = decryptedBytes.Skip(4);

                        messageType = BitConverter.ToInt32(tempbytes, 0);

                        if (messageType == ImageType)
                        {
                            imageData = ImageResult.FromPhotoshopResult(decryptedBytes.ToArray());
                        }
                        else if (messageType == ErrorstringType)
                        {
                            return(Encoding.UTF8.GetString(decryptedBytes.ToArray()));
                        }
                    }
                }

                //return "Message types: " + messageType;
                return("Image Received");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }