Code:
#ifdef __WIN32__
 #include <windows.h>
#else
 #include <netinet/in.h>
 #include <crypt.h>
#endif
#include <iostream>
#include <string>
#include <signal.h>
#include <cstdlib>
#include <stdio.h>

#include "commands.h"
#include "user.h"
#include "utils.h"
#include "config.h"
#include "sockets.h"
#include "trigger.h"
#include "defaults.h"
#include "ban.h"
#include "chatroom.h"
#include "elua.h"
#include "hublist.h"
#include "plugin.h"
#include "user_rights.h"

using namespace std;

int cont = 1;              // 1 = run / 0 = shutdown

extern user_t user_list;
extern command_t command_list;
extern config_t config;
extern account_t accounts_list;
extern config_elem_t config_list;

extern int port;                     // ID of port
extern int mainSocket;               // Main socket


/*************************************************************\
|               ARGUMENTS                      |
\*************************************************************/

#ifndef __WIN32__
/* This function was taken from aquila hubsoft and was modified */
void daemonize ()
{
    int i, lockfd;
      char pid[16];

    int ipid = getpid ();

      /* detach if asked */
      if (ipid == 1)
        return;            /* already a daemon */

        /* fork to guarantee we are not process group leader */
        i = fork ();
        if (i < 0)
              exit (1);            /* fork error */
        if (i > 0)
              exit (0);            /* parent exits */

        /* child (daemon) continues */
        setsid ();            /* obtain a new process group */
    ipid = getpid ()+1;

    printf ("> started with pid -> %d\n", ipid);
        /* fork again so we become process group leader 
          * and cannot regain a controlling tty 
          */
        i = fork ();
        if (i < 0)
              exit (1);            /* fork error */
        if (i > 0)
          exit (0);            /* parent exits */

        /* close all fds */
        for (i = getdtablesize (); i >= 0; --i)
              close (i);        /* close all descriptors */



        /* close parent fds and send output to fds 0, 1 and 2 to bitbucket */
        i = open ("/dev/null", O_RDWR);
        if (i < 0)
              exit (1);
        dup (i);
        dup (i);            /* handle standart I/O */

      /* create local lock */
      lockfd = open ("lamahub.pid", O_RDWR | O_CREAT, 0640);
      if (lockfd < 0) {
            perror ("lock: open");
            exit (1);
      }
    #ifndef __CYGWIN__
    /* lock the file */
    if (lockf (lockfd, F_TLOCK, 0) < 0) {
        perror ("lock: lockf");
        printf (HUBSOFT " is already running.\n");
        exit (0);
    }
    #else
    /* lock the file */
    {
    struct flock lock;
        lock.l_type = F_RDLCK;
        lock.l_start = 0;
        lock.l_whence = SEEK_SET;
        lock.l_len = 0;
        
        if (fcntl (lockfd, F_SETLK, &lock) < 0) {
            printf (HUBSOFT " is already running.\n");
            exit (0);
        }
    }
    #endif
    /* write to pid to lockfile */
    snprintf (pid, 16, "%d\n", getpid ());
    write (lockfd, pid, strlen (pid));
    


    /* restrict created files to 0750 */
    umask (027);
}
#endif

int arguments (int argc, char *argv[])
{
    string str;
    unsigned int i = 0;

    if (argc == 1)
        return 0;

    if (argc == 2) {
#ifndef __WIN32__
        if (!strcmp (argv[1], "-d")) {
            daemonize ();
            return 0;
        }
#endif
        if (!strcmp (argv[1], "-a")) {

            char name [128], passwd [128];
            printf ("Owner account\nYour owner name: ");
            scanf ("%s", name);
            printf ("Password: ");
            scanf ("%s", passwd);

            str += "regnum 1\n43520 44544 306689 306701 323149 323165\n";
            str += name;
            str += " ";
            str += crypt (passwd, HUBSOFT);
            str += " 5";
            i ++;
        }
    }
    if (argc == 3) {
        str += "regnum 1\n43520 44544 306689 306701 323149 323165\n";
        str += argv[1];
        str += " ";
        str += crypt (argv[2], HUBSOFT);
        str += " 5";
        i ++;
    }

       if (!i)
       {
        cerr << "> ERROR -> Syntax (create new owner account):" << endl;
            cerr << argv[0] << " <nick> <password>" << endl;
            return -1;
        }

    FILE *fp;

    char filename[80];
    filename[0] = '\0';
    strcpy (filename, DEFAULT_ACCOUNTSFILE);    

    fp = fopen (filename, "w+");

    if (!fp)
        return -2;
    
    fprintf (fp, "%s", str.c_str());

      fclose (fp);

    printf ("\n** Your Owner account was created. You can start hub now ! **\n");

    return 1;
}

/*************************************************************\
|               APP SIGNALS                      |
\*************************************************************/

void alarm_signal (int z)
{
    //cout << "> WARNING -> alarm signal -> type " << z << endl;
    //usleep (1000);
}

void term_signal (int z)
{
    cout << "> WARNING -> term signal -> type " << z << endl;

    usleep (1024);

    // close all sockets
    user_t *user;
    for (user = user_list.next; user != &user_list; user = user->next) {
        close (user->socket);
        //free (user);
    }

    /*config_elem_t *cfg;        // FIXME memleak in exit (calloc not free'ied)
    for (cfg = config_list.next; cfg != &config_list; cfg = cfg->next)
        if (cfg->value)
            free (cfg->value);*/

    cont = 0;    // Bye Bye :)
}

/*************************************************************\
|           INITIALIZATION OF APP              |
\*************************************************************/

int init_sig (void)
{
#ifndef __WIN32__
   struct sigaction sv;  
   
   memset(&sv, 0, sizeof(struct sigaction));
   sv.sa_flags = 0;
   sigemptyset(&sv.sa_mask);
/*#ifdef SA_NOCLDWAIT
   sv.sa_flags |= SA_NOCLDWAIT;
#endif
#ifdef SA_NOCLDSTOP
   sv.sa_flags |= SA_NOCLDSTOP;
#endif*/
   
   sv.sa_handler = SIG_IGN;
   /* Don't want broken pipes to kill the hub.  */
   sigaction(SIGPIPE, &sv, NULL);
   
   /* ...or any defunct child processes.  */
   sigaction(SIGCHLD, &sv, NULL);
   
   sv.sa_handler = term_signal;
   
   /* Also, shut down properly.  */
   sigaction(SIGTERM, &sv, NULL);
   sigaction(SIGINT, &sv, NULL);
   
   sv.sa_handler = alarm_signal;
   
   /* And set handler for the alarm call.  */
   sigaction(SIGALRM, &sv, NULL);
#endif
   return 1;
}


int init ()
{
    int ret = 0;

    ret += init_config ();
    ret += init_sig ();
    ret += init_commands ();
    ret += init_accounts ();
    ret += init_trigger ();
    ret += init_ban ();
    ret += init_chatroom ();
    ret += init_hublist ();
    ret += init_plugin ();
    ret += init_rights ();
#ifdef LUA
    ret += init_lua ();
#endif    

    char configfile [] = (DEFAULT_HUBFILE);

    /* We need load config at last, after initialization of all */
    config_load (configfile);

    /* Create main socket */
    ret += init_socket ();

    if (ret <= 0) {        // ERROR in initialization ??
        
        cerr << "> ERROR -> init ()" << endl;
        ret = 1;
        }

    return ret;
}

/*************************************************************\
|               MAIN THREAD                      |
\*************************************************************/

int main (int argc, char *argv[])
{
   if (arguments (argc, argv) != 0)
    exit (1);

   if (init () <= 0)
    cont = 0;

   for ( ; cont ; )    // MAIN LOOP
   {

    loop ();

   }

   cout << "> Shutdown" << endl;
   close(mainSocket);

   return 0;
}