MQTT failed with state -2

I have set WiFi credentials and connect over TCP data. WiFi is connecting by MQTT showing failed to connect with failed state -2 means connection failed. Also creating same name hotspot and mqtt subscribe topic via tcp server data. Here is the code sample

#include <ESP8266WiFi.h>
#include<SPI.h>
#include <PubSubClient.h>
 
#define LED 4

int port = 8888;
WiFiServer server(port);
 
//Enter your mqtt server configurations
const char* mqttServer = "xx.xx.xx.xx";    //Enter Your mqttServer address
const int mqttPort = 1883;       //Port number
const char* mqttUser = "xxxxxxx"; //User
const char* mqttPassword = "xxxxxxxx"; //Password
 
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
IPAddress staticIP(192,168,4,2);   
IPAddress gateway(192,168,4,1);   
IPAddress subnet(255,255,255,0);
 
void setup() {
  delay(500);
  pinMode(LED,OUTPUT);
  Serial.begin(115200);

//  WiFi.mode(WIFI_STA);
  WiFi.config(staticIP, gateway, subnet);  // (DNS not required)
  Serial.println(WiFi.SSID());
  Serial.println(WiFi.localIP());
  tcp_server_start();
 
}
 
void MQTTcallback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
 
  String message;
  for (int i = 0; i < length; i++) {
    message = message + (char)payload[i];  //Convert *byte to String
  }
   Serial.print(message);
  if(message == "#off") {digitalWrite(LED,LOW);}   //LED off
  if(message == "#on") {digitalWrite(LED,HIGH);} //LED on
 
  Serial.println();
  Serial.println("-----------------------");  
}
 
void loop() {
  Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
  delay(1000);
  if (mqtt_client.connected()){
    mqtt_client.loop();
  }
  WiFiClient client = server.available();
  if (client) {
    if(client.connected()){
      Serial.println("Client connected");
    }
    
    while(client.connected()){
      while(client.available()>0){
        // read data from the connected client
        String data = client.readStringUntil('\n');
        Serial.println(data);
        if (data.indexOf('|')>0 && data.indexOf(':')>0){
          String ssid = data.substring(0, data.indexOf("|"));
          String pass = data.substring(data.indexOf("|")+1, data.indexOf(":"));
          boolean res = wifi_connect(ssid.c_str(), pass.c_str());
          if (res == true){
            client.write("SUCCESS");
          } else {
            client.write("WRONG INPUT");  
          }
        }
        if (data.indexOf("SL1")>=0){
          const char* hotspot = data.c_str();
          if (WiFi.status() == WL_CONNECTED){
            client.write("SUCCESS");
            mqtt_connect(data.c_str());
            create_access_point(data.c_str());
          } else {
            client.write("WRONG INPUT");
          }
        } else if (data == "relay_on"){
          digitalWrite(LED,HIGH);
          client.write("SUCCESS");
        } else if (data == "relay_off"){
          digitalWrite(LED,LOW);
          client.write("SUCCESS");
        }
      }
    }
    client.stop();
    Serial.println("\nClient disconnected");    
  }
}

void create_access_point(const char* hotspot){
  Serial.print("Setting Soft Access Point ... ");
  boolean result = WiFi.softAP(hotspot, "88888888");

  if(result == true){
    Serial.println("Ready");
  } else{
    Serial.println("Failed!");
  }
}

boolean wifi_connect(const char* ssid, const char* password){
  WiFi.begin(ssid, password);
  delay(500);
  for (int i = 0; i<20; i++){
    if (WiFi.status() == WL_CONNECTED){
      Serial.println("Connected to WiFi");
      return true;
    }
    Serial.println("Connecting to WiFi...");
    delay(500);
  }
  return false;
}



void tcp_server_start(){
  server.begin();
  Serial.println("Open Telnet and connect to IP: ");
  Serial.println(WiFi.localIP());
  Serial.println("on port");
  Serial.println(port);
  create_access_point("ARNABESP8266");
}

void mqtt_connect(const char* topic_name){
  mqtt_client.setServer(mqttServer, mqttPort);
  mqtt_client.setCallback(MQTTcallback);
  delay(500);
  
  for (int i = 0; i<20; i++) {
    Serial.println("Connecting to MQTT...");
    mqtt_client.connect("ESP8266", mqttUser, mqttPassword);
      if (mqtt_client.connected()){
        Serial.println("connected");
        break;
      } else {
        Serial.print("failed with state ");
        Serial.println(mqtt_client.state());  //If you get state 5: mismatch in configuration
        delay(500);
 
    }
  }
 
  mqtt_client.publish(topic_name, "device_notification");
  mqtt_client.subscribe(topic_name);
}

void mqtt_loop(){
  mqtt_client.loop();
}

I want to set hotspot first then create TCP server listening, connect to WiFi and subscribe to the MQTT then change hotpost AP name as same as MQTT subscribe topic by sending over TCP data staring with string “SL”. How to fix this or any other way of doing it?

I replied to almost the same post before. Please do not post the same question multiple times!