void SendKeepalives()
 {
     if (isserver)
     {
         foreach (KeyValuePair <IPEndPoint, Level1ConnectionInfo> kvp in connections)
         {
             IPEndPoint           connection     = kvp.Key;
             Level1ConnectionInfo connectioninfo = kvp.Value;
             if ((int)DateTime.Now.Subtract(connectioninfo.LastOutgoingPacketTime).TotalMilliseconds > (KeepaliveIntervalSeconds * 1000))
             {
                 //  LogFile.WriteLine("sending keepalive to " + connection.ToString() );
                 Send(connection, new byte[] { 0 });
                 connectioninfo.UpdateLastOutgoingPacketTime();
             }
         }
     }
     else
     {
         Level1ConnectionInfo connectioninfo = connectiontoserver;
         if ((int)DateTime.Now.Subtract(connectioninfo.LastOutgoingPacketTime).TotalMilliseconds > (KeepaliveIntervalSeconds * 1000))
         {
             //LogFile.WriteLine("sending keepalive to server" );
             Send(new byte[] { 0 });
             connectioninfo.UpdateLastOutgoingPacketTime();
         }
     }
 }
 public void Send(IPEndPoint connection, byte[] data, int length)
 {
     if (connection != null)
     //if (isserver)
     {
         if (isserver)
         {
             LogFile.WriteLine("server layer1net.send( " + connection + " " + length);
         }
         else
         {
             LogFile.WriteLine("client layer1net.send( " + connection + " " + length);
         }
         if (connections.ContainsKey(connection))
         {
             connections[connection].UpdateLastOutgoingPacketTime();
         }
         try
         {
             udpclient.Send(data, data.Length, connection);
         }
         catch (Exception e)
         {
             LogFile.WriteLine(e);
             Init();
         }
     }
     else
     {
         LogFile.WriteLine("client layer1net.send( " + remoteserverendpoint + " " + length);
         connectiontoserver.UpdateLastOutgoingPacketTime();
         Send(remoteserverendpoint, data, length);
     }
 }