/// <summary>
 /// this method is continuesly waiting for new data on the networking stream,
 /// determines the type of the data and calls depending on the type methods to handle this data
 /// </summary>
 void reading()
 {
     try
     {
         while (asReading)
         {
             sendrecv.waitforstartpattern(_Reader); /* waiting for a startpattern(each packet starts with a start pattern.)
                                                     * the entire messaging protocol is defined and explained in the headers.cs file. */
             if (asReading)
             {
                 headers.packettype type = headers.packettypes[sendrecv.readfromstream(_Reader, 1)[0]]; /*this line reads the first byte of the packet and
                                                                                                         * converts it to a packet-type-enum-value*/
                 if (type == headers.packettype.Chat)
                 {
                     receiveChat();
                 }
                 if (type == headers.packettype.IMG)
                 {
                     receiveimg();
                 }
                 if (type == headers.packettype.LaserPointer)
                 {
                     receivelaserpointer();
                 }
                 if (type == headers.packettype.SessionInfo)
                 {
                     handlesessioninfo();
                 }
                 if (type == headers.packettype.File)
                 {
                     receiveFile();
                 }
                 if (type == headers.packettype.SessionPartnerHasConnectionProblems)
                 {
                     PartnerhasConnectionProblems();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         if (asReading)
         {
             throw new Exception($"Fehler beim asynchronen Lesen des netzwerkstreams: {ex.Message}");            /*ignoring exceptions which are thrown after the
                                                                                                                  * asynchronous reading was supposed to end. */
         }
     }
 }
        /// <summary>
        /// continuesly waits for a new packet and calls depending on the packet type a method to handle
        /// the packet.
        /// only stops when an exception occurs or when the Asyncreadflag is set to false.
        /// </summary>
        private void reading()
        {
            try
            {
                while (Asyncreadflag)
                {
                    //wait for a new packet
                    sendrecv.waitforstartpattern(reader);
                    headers.packettype type = headers.packettypes[sendrecv.readfromstream(reader, 1)[0]]; //check the type of the packet

                    //if packet is not a sessioninfo packet forward it without looking at it
                    if (type != headers.packettype.SessionInfo)
                    {
                        Int32  datalength = BitConverter.ToInt32(sendrecv.readfromstream(reader, 4), 0);
                        byte[] data       = sendrecv.readfromstream(reader, datalength);

                        foreach (Client c in session.clients)
                        {
                            if (c.role != this.role)
                            {
                                c.forward(type, data);
                                break;
                            }
                        }
                    }
                    //if the packet is a session info packet, the information is processed by the method handlesessioninfo
                    else if (type == headers.packettype.SessionInfo)
                    {
                        headers.infotype infotype = headers.infotypes[sendrecv.readfromstream(reader, 1)[0]];
                        handlesessioninfo(infotype);
                    }
                }
            }
            catch (Exception ex)
            {
                //ignore all exceptions after the moment this thread is supposed to end
                if (Asyncreadflag)
                {
                    Console.WriteLine(
                        $"Error in async reading for {role.ToString()} in session {this.session.SessionID}: {ex.Message}");
                }
            }
        }
 /// <summary>
 /// forwards a packet to the client
 /// </summary>
 /// <param name="type">the type of the packet</param>
 /// <param name="data">the content of the packet</param>
 public void forward(headers.packettype type, byte[] data)
 {
     try
     {
         //send start pattern and entire packet structure as defined in headers.cs
         sendrecv.writeOnStream(writer, headers.magicstartpattern);
         sendrecv.writeOnStream(writer, new byte[] { headers.packettypebytes[type] });
         if (type != headers.packettype.SessionInfo)
         {
             sendrecv.writeOnStream(writer, BitConverter.GetBytes(data.Length));
         }
         sendrecv.writeOnStream(writer, data);
     }
     catch (Exception ex)
     {
         if (ex is ConnectionNotWorkingException)
         {
             stopAsyncListening();
             handleConnectionProblems();
         }
     }
 }