// Common example connect logic
func CommonConnect(exampid, tag string, l *log.Logger) (net.Conn,
	*stompngo.Connection,
	error) {

	l.Printf("%stag:%s consess:%v common_connect_starts\n",
		exampid, tag, Lcs)

	// Set up the connection.
	h, p := senv.HostAndPort()
	hap := net.JoinHostPort(h, p)
	n, e := net.Dial("tcp", hap)
	if e != nil {
		return nil, nil, e
	}

	l.Printf("%stag:%s connsess:%s common_connect_host_and_port:%v\n",
		exampid, tag, Lcs,
		hap)

	// Create connect headers and connect to stompngo
	ch := ConnectHeaders()
	l.Printf("%stag:%s connsess:%s common_connect_headers headers:%v\n",
		exampid, tag, Lcs,
		ch)
	conn, e := stompngo.Connect(n, ch)
	if e != nil {
		return nil, conn, e
	}
	l.Printf("%stag:%s connsess:%s common_connect_complete host:%s port:%s vhost:%s protocol:%s server:%s\n",
		exampid, tag, conn.Session(),
		h, p, senv.Vhost(), conn.Protocol(), ServerIdent(conn))

	// Show connect response
	l.Printf("%stag:%s connsess:%s common_connect_response connresp:%v\n",
		exampid, tag, conn.Session(),
		conn.ConnectResponse)

	// Heartbeat Data
	l.Printf("%stag:%s connsess:%s common_connect_heart_beat_send hbsend:%d\n",
		exampid, tag, conn.Session(),
		conn.SendTickerInterval())
	l.Printf("%stag:%s connsess:%s common_connect_heart_beat_recv hbrecv:%d\n",
		exampid, tag, conn.Session(),
		conn.ReceiveTickerInterval())

	l.Printf("%stag:%s connsess:%s common_connect_local_addr:%s\n",
		exampid, tag, conn.Session(),
		n.LocalAddr().String())
	l.Printf("%stag:%s connsess:%s common_connect_remote_addr:%s\n",
		exampid, tag, conn.Session(),
		n.RemoteAddr().String())

	//
	return n, conn, nil
}
func ShowRunParms(exampid string) {
	llu.Printf("%sHOST:%v\n", exampid, os.Getenv("STOMP_HOST"))
	llu.Printf("%sPORT:%v\n", exampid, os.Getenv("STOMP_PORT"))
	llu.Printf("%sPROTOCOL:%v\n", exampid, senv.Protocol())
	llu.Printf("%sVHOST:%v\n", exampid, senv.Vhost())
	llu.Printf("%sNQS:%v\n", exampid, Nqs())
	llu.Printf("%sNMSGS:%v\n", exampid, senv.Nmsgs())
	llu.Printf("%sSUBCHANCAP:%v\n", exampid, senv.SubChanCap())
	llu.Printf("%sRECVFACT:%v\n", exampid, RecvFactor())
	llu.Printf("%sSENDFACT:%v\n", exampid, SendFactor())
	llu.Printf("%sRECVWAIT:%t\n", exampid, RecvWait())
	llu.Printf("%sSENDWAIT:%t\n", exampid, SendWait())
	llu.Printf("%sACKMODE:%v\n", exampid, AckMode())
}
// Provide connect headers
func ConnectHeaders() stompngo.Headers {
	h := stompngo.Headers{}
	l := senv.Login()
	if l != "" {
		h = h.Add("login", l)
	}
	pc := senv.Passcode()
	if pc != "" {
		h = h.Add("passcode", pc)
	}
	//
	p := senv.Protocol()
	if p != stompngo.SPL_10 { // 1.1 and 1.2
		h = h.Add("accept-version", p).Add("host", senv.Vhost())
		hb := senv.Heartbeats()
		if hb != "" {
			h = h.Add("heart-beat", hb)
		}
	}
	//

	return h
}
// Common example TLS connect logic
func CommonTLSConnect(exampid, tag string, l *log.Logger,
	c *tls.Config) (net.Conn, *stompngo.Connection, error) {

	l.Printf("%stag:%s consess:%s common_tls_connect_starts\n",
		exampid, tag, Lcs)

	// Set up the connection.
	h, p := senv.HostAndPort()
	hap := net.JoinHostPort(h, p)
	n, e := net.Dial("tcp", hap)
	if e != nil {
		return nil, nil, e
	}

	c.ServerName = h // SNI

	nc := tls.Client(n, c) // Returns: *tls.Conn : implements net.Conn
	e = nc.Handshake()
	if e != nil {
		if e.Error() == "EOF" {
			l.Printf("%stag:%s consess:%s common_tls_handshake_EOF_Is_the_broker_port_TLS_enabled? port:%s\n",
				exampid, tag, Lcs,
				p)
		}
		l.Fatalf("%stag:%s consess:%s common_tls_handshake_failed error:%v\n",
			exampid, tag, Lcs,
			e.Error())
	}
	l.Printf("%stag:%s consess:%s common_tls_handshake_complete\n",
		exampid, tag, Lcs)

	l.Printf("%stag:%s connsess:%s common_tls_connect_host_and_port:%v\n",
		exampid, tag, Lcs,
		hap)

	// Create connect headers and connect to stompngo
	ch := ConnectHeaders()
	l.Printf("%stag:%s connsess:%s common_tls_connect_headers headers:%v\n",
		exampid, tag, Lcs,
		ch)
	conn, e := stompngo.Connect(nc, ch)
	if e != nil {
		return nil, nil, e
	}
	l.Printf("%stag:%s connsess:%s common_tls_connect_complete host:%s vhost:%s protocol:%s server:%s\n",
		exampid, tag, conn.Session(),
		h, senv.Vhost(), conn.Protocol(), ServerIdent(conn))

	// Show connect response
	l.Printf("%stag:%s connsess:%s common_tls_connect_response connresp:%v\n",
		exampid, tag, conn.Session(),
		conn.ConnectResponse)

	// Show heartbeat data (if heart beats are being used)
	if senv.Heartbeats() != "" {
		l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_send hbsend:%v\n",
			exampid, tag, conn.Session(),
			conn.SendTickerInterval())
		l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_recv hbrecv:%v\n",
			exampid, tag, conn.Session(),
			conn.ReceiveTickerInterval())
	}

	l.Printf("%stag:%s connsess:%s common_tls_connect_local_addr:%s\n",
		exampid, tag, conn.Session(),
		n.LocalAddr().String())
	l.Printf("%stag:%s connsess:%s common_tls_connect_remote_addr:%s\n",
		exampid, tag, conn.Session(),
		n.RemoteAddr().String())

	//
	return nc, conn, nil
}