    server.begin();
 }
 
+//
+char getstatus()
+{
+//
+   char result = 0;
+   for (int i = 0; i < pinc; i++)
+      if (pineepromstate[i] == 1) result &= 1 << i;
+   return result;
+}
+
 // the loop routine runs over and over again forever:
 void loop() 
 {
-//   
+//
+   //process up-down events
+   for (int i = 0; i < pinc; i++)
+      if (pinsoftstate[i]==1)
+         {
+      //
+         EEPROM.write(eeprom[i], 1);
+         pineepromstate[i] = 1;
+         pinsoftstate[i] = 0;
+         pinreport[i] = 0;
+         }
+
+   //down always send to client
    for (int i = 0; i < pinc; i++)
       {
    //
-      //анализ pinsoftstate? без pinreport? где сообщать клиенту?
-      if (pinhardstate[i] == 1 && pinsoftstate[i]==1 && pinreport[i]==0)
+       if (pinreport[i] == 0 && pineepromstate[i]==1)
          {
       //
-         server.println();
-         server.print("pin down["); server.print(i, DEC); server.print("]");
-            
+         server.write( getstatus() );
          pinreport[i] = 1;
          }
-      else
-         if (pinhardstate[i] == 0 && pinsoftstate[i]==1 && pinreport[i]==1)
+      }
+
+   //process command from station
+   while (EthernetClient client = server.available())
+      while (client.available())
+         {
+      //
+         char cmd = client.read();
+         if (cmd & TSTMODE)
             {
          //
-            server.println();
-            server.print("pintime["); server.print(i, DEC); server.print("]="); server.print(pintime[i], DEC);
-            
-            pinsoftstate[i] = 0;
-            pinreport[i] = 0;
+            cmd &= GETSTAT;
             }
-      }
+         //else
+         if (cmd & DORESET)
+            {
+         //
+            for (int i = 0; i < pinc; i++)
+               {
+            //
+               EEPROM.write(eeprom[i], 0);
+               pineepromstate[i] = 0;
+               }
+            }
+         //else
+         if (cmd & GETSTAT)
+            {
+         //
+            server.write(cmd & getstatus());
+            }
+         }
 }

      }

   //
   Timer1.initialize(100000); // 10Hz // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz)
   Timer1.attachInterrupt( timerIsr );

   //Start up the serial port

const int pins[pinc] = {5,6,7,8}; //physical to logical pin address
const int eeprom[pinc] = {0,1,2,3}; //pin address in eeprom

volatile int pinhardstate[pinc] = {0,0,0,0};
volatile int pinsoftstate[pinc] = {0,0,0,0};
int pineepromstate[pinc] = {0,0,0,0};
int pinreport[pinc] = {0,0,0,0};

byte mac[] = {0x02, 0x80, 0xC2, 0x00, 0x00, 0x01};
