public static void HandleGetDesktop(GetDesktop command, Client client)
        {
            // TODO: Capture mouse in frames: https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis
            var monitorBounds = ScreenHelper.GetBounds((command.DisplayIndex));
            var resolution    = new Resolution {
                Height = monitorBounds.Height, Width = monitorBounds.Width
            };

            if (StreamCodec == null)
            {
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.DisplayIndex, resolution);
            }

            if (command.CreateNew || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.DisplayIndex ||
                StreamCodec.Resolution != resolution)
            {
                if (StreamCodec != null)
                {
                    StreamCodec.Dispose();
                }

                StreamCodec = new UnsafeStreamCodec(command.Quality, command.DisplayIndex, resolution);
            }

            BitmapData desktopData = null;
            Bitmap     desktop     = null;

            try
            {
                desktop     = ScreenHelper.CaptureScreen(command.DisplayIndex);
                desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height),
                                               ImageLockMode.ReadWrite, desktop.PixelFormat);

                using (MemoryStream stream = new MemoryStream())
                {
                    if (StreamCodec == null)
                    {
                        throw new Exception("StreamCodec can not be null.");
                    }
                    StreamCodec.CodeImage(desktopData.Scan0,
                                          new Rectangle(0, 0, desktop.Width, desktop.Height),
                                          new Size(desktop.Width, desktop.Height),
                                          desktop.PixelFormat, stream);
                    client.Send(new GetDesktopResponse
                    {
                        Image      = stream.ToArray(),
                        Quality    = StreamCodec.ImageQuality,
                        Monitor    = StreamCodec.Monitor,
                        Resolution = StreamCodec.Resolution
                    });
                }
            }
            catch (Exception)
            {
                if (StreamCodec != null)
                {
                    client.Send(new GetDesktopResponse
                    {
                        Image      = null,
                        Quality    = StreamCodec.ImageQuality,
                        Monitor    = StreamCodec.Monitor,
                        Resolution = StreamCodec.Resolution
                    });
                }

                StreamCodec = null;
            }
            finally
            {
                if (desktop != null)
                {
                    if (desktopData != null)
                    {
                        try
                        {
                            desktop.UnlockBits(desktopData);
                        }
                        catch
                        {
                        }
                    }
                    desktop.Dispose();
                }
            }
        }
        private void Execute(ISender client, GetDesktop message)
        {
            // TODO: Switch to streaming mode without request-response once switched from windows forms
            // TODO: Capture mouse in frames: https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis
            var monitorBounds = ScreenHelper.GetBounds((message.DisplayIndex));
            var resolution    = new Resolution {
                Height = monitorBounds.Height, Width = monitorBounds.Width
            };

            if (_streamCodec == null)
            {
                _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);
            }

            if (message.CreateNew || _streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex ||
                _streamCodec.Resolution != resolution)
            {
                _streamCodec?.Dispose();

                _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);
            }

            BitmapData desktopData = null;
            Bitmap     desktop     = null;

            try
            {
                desktop     = ScreenHelper.CaptureScreen(message.DisplayIndex);
                desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height),
                                               ImageLockMode.ReadWrite, desktop.PixelFormat);

                using (MemoryStream stream = new MemoryStream())
                {
                    if (_streamCodec == null)
                    {
                        throw new Exception("StreamCodec can not be null.");
                    }
                    _streamCodec.CodeImage(desktopData.Scan0,
                                           new Rectangle(0, 0, desktop.Width, desktop.Height),
                                           new Size(desktop.Width, desktop.Height),
                                           desktop.PixelFormat, stream);
                    client.Send(new GetDesktopResponse
                    {
                        Image      = stream.ToArray(),
                        Quality    = _streamCodec.ImageQuality,
                        Monitor    = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }
            }
            catch (Exception)
            {
                if (_streamCodec != null)
                {
                    client.Send(new GetDesktopResponse
                    {
                        Image      = null,
                        Quality    = _streamCodec.ImageQuality,
                        Monitor    = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }

                _streamCodec = null;
            }
            finally
            {
                if (desktop != null)
                {
                    if (desktopData != null)
                    {
                        try
                        {
                            desktop.UnlockBits(desktopData);
                        }
                        catch
                        {
                        }
                    }
                    desktop.Dispose();
                }
            }
        }