 
 int changeSsidName(char *_sSsidName)
 {
-//    char    sSsidName[LOCAL_BUFFER_SIZE];
-
   // Truncate Ssid if it too long
-  // SSID_MAX_LENGTH
+  snprintf(_sSsidName,SSID_MAX_LENGTH,"%s",_sSsidName);
 
 // We need a buffer to read in data
   char      Buffer[LOCAL_BUFFER_SIZE];
+  char      TempBuffer[LOCAL_BUFFER_SIZE];
 
   // Open the file for reading/write.
-  FILE     *Input = fopen(HOSTAPD_CONFIG_FILE_FULL_PATH, "r+"); // Read/write
+  FILE     *Input = fopen(HOSTAPD_CONFIG_FILE_FULL_PATH, "r"); // Read/write
+  FILE     *Output = fopen(TEMP_CONFIG_FILE_FULL_PATH, "w"); // Read/write
 
   // Our find and replace arguments
   char     *Find = "ssid=";
   char    sSsidLine[LOCAL_BUFFER_SIZE];
 
-//  Serial.println("Create SSID:    ");
   // Create file line    
-  sprintf(sSsidLine, "ssid=%s", _sSsidName);
+  sprintf(sSsidLine,"ssid=%s\n", _sSsidName);
   
   if(NULL == Input)
   {
-      trace_info("%s(): /etc/hostapd/hostapd.conf File not found", __func__);
+      trace_info("%s(): %s file not found", __func__,HOSTAPD_CONFIG_FILE_FULL_PATH);
       return 1;
   }
-
-//  Serial.print("Find:    ");     Serial.println(Find);
 
   // For each line...
   while(NULL != fgets(Buffer, LOCAL_BUFFER_SIZE, Input))
   {
     char *Start = NULL;    // Where 'ssid=' was found in the line
     char *Line = Buffer; // Start at the beginning of the line, and after each match
-    
- //   Serial.print("Line: "); Serial.println(Buffer);
-    
-      // Find match
-    Start = strstr(Line, Find);
-    if( Start )
-    {
- //     Serial.print("Write: "); Serial.println(sSsidLine);
-      fwrite(sSsidLine, 1, strlen(sSsidLine), Input);
+     
+    // Find match
+    if( strstr(Line, Find) )
+    {
+      fputs(sSsidLine, Output);
+    }
+    else
+    {
+      fputs(Line, Output);      
     }
   }
 
   // Close our files
   fclose(Input);
+  fclose(Output);
+  
+  rename(TEMP_CONFIG_FILE_FULL_PATH,HOSTAPD_CONFIG_FILE_FULL_PATH);
   
   return 0;
 }

/*
    g_iCatNumber++;
    char sSsidName[100];
    sprintf(sSsidName, "CAT%i", g_iCatNumber);
    changeSsidName(sSsidName);
 */ 
    //////////////////////////////////////////  

    last_print = millis();

  if (Serial.available() > 0) {
    // get incoming byte:
    g_iByte = Serial.read();

    switch (g_iByte)
    {
