void
CommunicationObject::Main ()
{

  OutputModule *output_module;
  output_module = OutputModule::Instance ();
  
  ApplicationSetup* comm_object_application_setup;
  comm_object_application_setup = ApplicationSetup::Instance ();
  
  LogFile *logfile;
  logfile = LogFile::Instance ();

  int client_sock;
  int c;
  struct sockaddr_in server, client;
  int socket_desc;
  socket_desc = socket (AF_INET, SOCK_STREAM, 0);

  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr(SERVER_IP);
  server.sin_port = htons (SERVER_PORT);

  if (FLUSH_OUTPUTMODULETCP == 1)
  {
	  int flag1 = 1;
	  int result1 = setsockopt(client_sock,   /* socket affected */
							IPPROTO_TCP,     /* set option at TCP level */
							TCP_NODELAY,     /* name of option */
							(char *) &flag1,  /* the cast is historical cruft */
							sizeof(int));    /* length of option value */
  }

  /*Binding the socket*/
  bind (socket_desc, (struct sockaddr *) &server, sizeof (server));

  while (go)
    {

      listen (socket_desc, 1);

      c = sizeof (struct sockaddr_in);

      if (go == 0)
	{
	  break;
	}

   client_sock = 
   accept (socket_desc, (struct sockaddr *) &client, (socklen_t *) & c);

	if (FLUSH_OUTPUTMODULETCP == 1)
	{
		int flag = 1;
		int result = setsockopt(client_sock,  /* socket affected */
                        IPPROTO_TCP,     /* set option at TCP level */
                        TCP_NODELAY,     /* name of option */
                        (char *) &flag,  /* the cast is historical cruft */
                        sizeof(int));    /* length of option value */
	}

      if (go == 0)
	{
	  break;
	}

      if (client_sock >= 0)
	{
	  output_module->OutputModuleStdoutOff();
	  output_module->OutputModuleSockidOn(client_sock);
	  output_module->TcpUserArrayInsert (client_sock);
	  output_module->Output("Welcome: Comunicazione col server stabilita\n");
	  
      //Warning: all the following functions like LogFileWriteString will write
      //on the log file specified by LogFileSet. If you want to write on another file you have
      //to use again the function LogFileSet.	  
	  logfile->LogFileSet(comm_object_application_setup->application_setup_general_log_path);
	  string tmp_address = inet_ntoa(client.sin_addr);
	  logfile->LogFileWriteString(GetTime() + ": Assigned socket id " + to_string(client_sock) + " to client with ip " + tmp_address + "\n\n");
	  
	  worker_thread = new thread (&CommunicationObject::Worker, this,
			  (void *) &client_sock);
      worker_thread->detach();
	}

    }   // while go

}
void 
CommunicationObject::Worker (void *socket_desc)
{
  OutputModule *output_module;
  output_module = OutputModule::Instance ();

  ApplicationSetup* comm_object_application_setup;
  comm_object_application_setup = ApplicationSetup::Instance ();
  
  LogFile *logfile;
  logfile = LogFile::Instance ();

  int j;
  int sock = *(int *) socket_desc;
  int read_size;
  char buffer[STANDARDBUFFERLIMIT];
  const char *my_punt;
  bzero (buffer, STANDARDBUFFERLIMIT);

  while (go)
    {
      bzero (buffer, STANDARDBUFFERLIMIT);
      if ((read_size = recv (sock, buffer, STANDARDBUFFERLIMIT, MSG_NOSIGNAL)) <= 0)
		{
	      //Warning: all the following functions like LogFileWriteString will write
	      //on the log file specified by LogFileSet. If you want to write on another file you have
	      //to use again the function LogFileSet.			
		  logfile->LogFileSet(comm_object_application_setup->application_setup_general_log_path);
		  logfile->LogFileWriteString(GetTime() + ": User with socket id " + to_string(sock) + " disconnected\n\n");
		  
		  output_module->TcpUserArrayDelete (sock);
		  return;
		}

      string tmp(buffer);
      auto command_list = explode(tmp, '\n');
      int i = 0;

	  for ( i = 0; i < command_list.size() ; i ++)
	  {
		  unique_lock<mutex> ReservedKeyBoardInputAreaHandle(ReservedKeyBoardInputArea);

		  if (num_mex == MAXCOMMAND)
			BlockedProducerInput.wait(ReservedKeyBoardInputAreaHandle);
			
		  coda = (coda + 1) % MAXCOMMAND;
		  //strncpy (command[coda].command_sent_by_user, buffer, STANDARDBUFFERLIMIT - 1);
		  strncpy (command[coda].command_sent_by_user, command_list[i].c_str(), STANDARDBUFFERLIMIT - 1);
		  command[coda].user_sockid = sock;
		  
		  num_mex++;

		  BlockedConsumerInput.notify_one();
	  }


    }    // while go

      BlockedConsumerInput.notify_one();

}