private void HandleHeartbeat()
        {
            _heartbeatSocket = new RouterSocket(_producerConfiguration.HeartbeatEnpoint);

            while (!_cancel.IsCancellationRequested)
            {
                var message      = _heartbeatSocket.ReceiveMultipartMessage();
                var messageBytes = message[2].Buffer;

                var heartbeat = JsonConvert.DeserializeObject <HeartbeatQuery>(Encoding.UTF32.GetString(messageBytes), _settings);

                using (var sender = new RequestSocket(heartbeat.HeartbeatEndpoint))
                {
                    var heartbeatResponse = new HeartbeatResponse()
                    {
                        ProducerId = _producerConfiguration.Id,
                        Now        = DateTime.Now
                    };

                    var msg = Encoding.UTF32.GetBytes(JsonConvert.SerializeObject(heartbeatResponse, _settings));

                    sender.SendFrame(msg);

                    Task.Delay(200).Wait();
                }
            }
        }
        public bool HandleHeartbeat()
        {
            var failures = 0;

            _heartbeatSocket = new RouterSocket(_consumerConfiguration.HeartbeatEndpoint);

            while (!_cancel.IsCancellationRequested || failures > HeartbeatFailureTolerance)
            {
                using (var heartbeatQuery = new RequestSocket(_currentProducer.HeartbeatEndpoint))
                {
                    var heartbeat = new HeartbeatQuery()
                    {
                        HeartbeatEndpoint = _consumerConfiguration.HeartbeatEndpoint
                    };

                    var msg = Encoding.UTF32.GetBytes(JsonConvert.SerializeObject(heartbeat, _settings));

                    heartbeatQuery.SendFrame(msg);

                    Thread.Sleep(HeartbeatDelay);

                    var heartbeatMessage = new NetMQMessage();

                    if (_heartbeatSocket.TryReceiveMultipartMessage(ref heartbeatMessage))
                    {
                        _lastHearbeat = JsonConvert.DeserializeObject <HeartbeatResponse>(Encoding.UTF32.GetString(heartbeatMessage[2].Buffer), _settings);
                        failures      = 0;
                    }
                    else
                    {
                        failures++;
                    }

                    if (failures > HeartbeatFailureTolerance)
                    {
                        break;
                    }
                }
            }

            return(failures < HeartbeatFailureTolerance);
        }