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 SendImage( ImageResult imageData )
		{
			if ( !_connection.Connect() )
			{
				return "Error connecting to Photoshop.";
			}

			var stream = _connection.Stream;

			int width = imageData.Width;
			int height = imageData.Height;

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

			try
			{
				/* WRITE */
				var temp_stream = new MemoryStream();
				var w = new BinaryWriter( temp_stream );

				w.Write( SwapEndian( ProtocolVersion ) );
				w.Write( SwapEndian( _transactionId++ ) );
				w.Write( SwapEndian( ImageType ) );

				w.Write( ( byte ) 2 ); 				//imagetype (2 for PixMap);	
				w.Write( SwapEndian( width ) );
				w.Write( SwapEndian( height ) );
				w.Write( SwapEndian( width * 3 ) );	//bytesPerScanLine
				w.Write( ( byte ) 1 ); 				//color mode RGB
				w.Write( ( byte ) 3 ); 				//channel count
				w.Write( ( byte ) 8 ); 				//bits per channel

				var data = imageData.GenerateRGBData();
				w.Write( data, 0, data.Length );
				w.Flush();

				long len = temp_stream.Length;
				temp_stream.Seek( 0, SeekOrigin.Begin );
				BinaryReader r = new BinaryReader( temp_stream );
				var message = new byte[ len ];
				r.Read( message, 0, ( int ) len );

				var encryptedMessage = _encryptDecrypt.encrypt( message );
				int messageLength = CommLength + encryptedMessage.Length;

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

				// Photoshop CS6 will issue a response, make sure we skip it (small timeout)
				DateTime start = DateTime.Now;
				while ( ( DateTime.Now - start ).TotalMilliseconds < 100 )
				{
					if ( !stream.DataAvailable )
						continue;

					var buf = new byte[ 4 ];
					stream.Read( buf, 0, 4 );
					int length = BitConverter.ToInt32( buf.Reverse().ToArray(), 0 );

					buf = new byte[ length ];
					stream.Read( buf, 0, length );
				}

				return "Sent";
			}
			catch ( Exception e )
			{
				//Debug.Log( e.Message );
				return e.Message;
			}
		}
        string SendTestImage( PhotoshopProtocol pp )
        {
            string result = "";

            result += FlushRead();

            byte[] image = {
            0x02,
            0x00, 0x00, 0x00, 0x01,
            0x00, 0x00, 0x00, 0x01,
            0x00, 0x00, 0x00, 0x01,
            0x01,
            0x03,
            0x08,
            0xff, 0x00, 0x00 };

            MemoryStream temp_stream = new MemoryStream();
            BinaryWriter bw = new BinaryWriter( temp_stream );

            bw.Write( SwapEndian( PhotoshopProtocol.ProtocolVersion ) );	// Protocol version
            bw.Write( SwapEndian( pp._transactionId++ ) );					// Transaction ID
            bw.Write( SwapEndian( 3 ) );									// Content Type
            bw.Write( image );												// Image data
            bw.Flush();

            long len = temp_stream.Length;
            temp_stream.Seek( 0, SeekOrigin.Begin );

            BinaryReader br = new BinaryReader( temp_stream );

            var message = new byte[ len ];
            br.Read( message, 0, ( int ) len );

            string password = EditorPrefs.GetString( "AmplifyColor.NetworkPassword", "password" );
            EncryptDecrypt encryptDecrypt = new EncryptDecrypt( password );
            var encryptedMessage = encryptDecrypt.encrypt( message );

            //string str = "";
            //for (int i = 0; i < message.Length; i++)
            //{
            //	str += ( ( message[i] & 0xff ) + 0x100 ).ToString( "x" ).Substring( 1 ) + " ";
            //	if (i > 0 && (((i+1) % 8) == 0))
            //		str += "\n";
            //}
            //Debug.Log( str );
            //str = "";
            //for (int i = 0; i < encryptedMessage.Length; i++)
            //{
            //	str += ( ( encryptedMessage[i] & 0xff ) + 0x100 ).ToString( "x" ).Substring( 1 ) + " ";
            //	if (i > 0 && (((i+1) % 8) == 0))
            //		str += "\n";
            //}
            //Debug.Log( str );

            int messageLength = PhotoshopProtocol.CommLength + encryptedMessage.Length;
            _stream.Write( BitConverter.GetBytes( messageLength ).Reverse().ToArray(), 0, 4 );
            _stream.Write( BitConverter.GetBytes( PhotoshopProtocol.NoCommError ).Reverse().ToArray(), 0, 4 );
            _stream.Write( encryptedMessage, 0, encryptedMessage.Length );
            _stream.Flush();

            System.Threading.Thread.Sleep( 1000 );

            result += FlushRead();

            return result;
        }
		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;
			}
		}
        private string SendTestImage(PhotoshopProtocol pp)
        {
            string result = "";

            result += FlushRead();

            byte[] image =
            {
                0x02,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x01,
                0x01,
                0x03,
                0x08,
                0xff, 0x00, 0x00
            };

            MemoryStream temp_stream = new MemoryStream();
            BinaryWriter bw          = new BinaryWriter(temp_stream);

            bw.Write(SwapEndian(PhotoshopProtocol.ProtocolVersion));    // Protocol version
            bw.Write(SwapEndian(pp._transactionId++));                  // Transaction ID
            bw.Write(SwapEndian(3));                                    // Content Type
            bw.Write(image);                                            // Image data
            bw.Flush();

            long len = temp_stream.Length;

            temp_stream.Seek(0, SeekOrigin.Begin);

            BinaryReader br = new BinaryReader(temp_stream);

            var message = new byte[len];

            br.Read(message, 0, (int)len);

            string         password         = EditorPrefs.GetString("AmplifyColor.NetworkPassword", "password");
            EncryptDecrypt encryptDecrypt   = new EncryptDecrypt(password);
            var            encryptedMessage = encryptDecrypt.encrypt(message);

            //string str = "";
            //for (int i = 0; i < message.Length; i++)
            //{
            //	str += ( ( message[i] & 0xff ) + 0x100 ).ToString( "x" ).Substring( 1 ) + " ";
            //	if (i > 0 && (((i+1) % 8) == 0))
            //		str += "\n";
            //}
            //Debug.Log( str );
            //str = "";
            //for (int i = 0; i < encryptedMessage.Length; i++)
            //{
            //	str += ( ( encryptedMessage[i] & 0xff ) + 0x100 ).ToString( "x" ).Substring( 1 ) + " ";
            //	if (i > 0 && (((i+1) % 8) == 0))
            //		str += "\n";
            //}
            //Debug.Log( str );

            int messageLength = PhotoshopProtocol.CommLength + encryptedMessage.Length;

            _stream.Write(BitConverter.GetBytes(messageLength).Reverse().ToArray(), 0, 4);
            _stream.Write(BitConverter.GetBytes(PhotoshopProtocol.NoCommError).Reverse().ToArray(), 0, 4);
            _stream.Write(encryptedMessage, 0, encryptedMessage.Length);
            _stream.Flush();

            System.Threading.Thread.Sleep(1000);

            result += FlushRead();

            return(result);
        }