private void SocketListener()
        {
            while (_connectionActive && _client.Connected)
            {
                if (_client.GetStream().DataAvailable)
                {
                    // Read Data from stream
                    var optionsSizeBuffer = new byte[4];
                    var contentSizeBuffer = new byte[4];

                    _client.GetStream().Read(optionsSizeBuffer, 0, 4);
                    _client.GetStream().Read(contentSizeBuffer, 0, 4);

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(optionsSizeBuffer, 0, optionsSizeBuffer.Length);
                        Array.Reverse(contentSizeBuffer, 0, contentSizeBuffer.Length);
                    }

                    var optionsSize = (uint)BitConverter.ToInt32(optionsSizeBuffer, 0);
                    var contentSize = (uint)BitConverter.ToInt32(contentSizeBuffer, 0);

                    var optionsBuffer = new byte[optionsSize];
                    var contentBuffer = new byte[contentSize];

                    // Dirty typecast (OH NOEZ)
                    _client.GetStream().Read(optionsBuffer, 0, (int)optionsSize);
                    _client.GetStream().Read(contentBuffer, 0, (int)contentSize);

                    var optionsText = System.Text.Encoding.UTF8.GetString(optionsBuffer);
                    var contentText = System.Text.Encoding.UTF8.GetString(contentBuffer);

                    var optionsDoc = new XmlDocument();
                    optionsDoc.LoadXml(optionsText);
                    var contentDoc = new XmlDocument();
                    contentDoc.LoadXml(contentText);

                    var resultMessage = new ServerMessage();
                    try
                    {
                        resultMessage.Id = Convert.ToUInt32(optionsDoc.SelectSingleNode(@"/options/id").InnerText);
                        resultMessage.MessageType = (Enums.MessageType)Convert.ToInt32(optionsDoc.SelectSingleNode(@"/options/messageType").InnerText);
                        var commandInnerText = optionsDoc.SelectSingleNode(@"/options/command").InnerText;
                        if (!string.IsNullOrEmpty(commandInnerText)) resultMessage.CommandType = (Enums.CommandType)Convert.ToUInt32(commandInnerText);
                        var channelName = optionsDoc.SelectSingleNode(@"/options/channel").InnerText;
                        var existingChannels = KnownChannels.Where(p => p.Name == channelName);

                        if (existingChannels.Any())
                            resultMessage.Channel = existingChannels.First();
                        else
                        {
                            var channel = new Channel() { IsOpened = false, Name = channelName, MessageLog = new MessageLog() };
                            resultMessage.Channel = channel;
                            KnownChannels.Add(channel);
                        }

                        resultMessage.User = optionsDoc.SelectSingleNode(@"/options/user").InnerText;
                        resultMessage.MessageContent = contentDoc.SelectSingleNode(@"/message").InnerText;
                        resultMessage.OptionsSize = optionsSize;
                        resultMessage.ContentSize = contentSize;
                    }
                    catch(Exception ex)
                    {
                        // Corrupt document -- ignore
                        DiContainer.Container.Resolve<ILoggingManager>().LogError("An error occurred while trying to parse response document from server.", ex);
                    }

                    IncomingMessages.Add(resultMessage);

                    foreach (var subscriber in _subscribers)
                    {
                        subscriber.OnServerMessageReceived();
                    }
                }
                Thread.Sleep(100);
            }
        }
 private void JoinChannelBtn_Click(object sender, RoutedEventArgs e)
 {
     channel = new Channel() { Name = JoinChannelText.Text, MessageLog = new MessageLog(), IsOpened = true };
     var message = new MessageBuilder().BuildCommand(Enums.CommandType.GetChannels, channel, "jug-eared");
     server.SendMessage(message);
 }