func (cm *ConnManager) ListenAndServe(addr string) error {
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		panic(err)
	}
	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatalf("Listen error: %s", err)
	}
	go cm.cleanUpCHPoolWithCCID()
	go cm.cleanUpCHPoolWithClientID()
	go cm.dispatch()
	newConnChannel := make(chan io.ReadWriteCloser)
	go cm.handleNewConn(newConnChannel)
	go func() {
		select {
		case <-cm.quit:
			log.Infof("CM: Received quit signal, listener info: %s", listener.Addr())
			close(newConnChannel)
			listener.Close()
		}
	}()
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			cm.mutex.Lock()
			if cm.stopped {
				cm.mutex.Unlock()
				return nil
			}
			log.Errorf("CM: Accept Connection Error: %s", err)
			cm.mutex.Unlock()
			return err
		}
		log.Debugf("CM: Received new connection from %s", conn.RemoteAddr())
		newConnChannel <- conn
	}
}
ListenAndServe listens for addr with port and handles connections coming from them.
This function can be both client and server
package gota
import (
	"container/list"
	"crypto/rand"
	"encoding/binary"
	"io"
	"math"
	"net"
	"sync"
	"time"
	log "github.com/Sirupsen/logrus"
)
type CCID uint64
func NewCCID(clientID ClientID, connID uint32) (cc CCID) {
	c := uint64(clientID)
	cc = CCID(c<<32 + uint64(connID))
	return
}
func (cc CCID) ClientID() ClientID {
	return uint32(cc >> 32)
}
func (cc CCID) ConnID() uint32 {
	return uint32(cc & 0x00000000FFFFFFFF)
}
type ConnManager struct {
	clientID ClientID
	newCCIDChannel        chan CCID
	cleanUpCHChanCCID     chan CCID
	cleanUpCHChanClientID chan ClientID
	poolLock        *sync.RWMutex
	connHandlerPool map[CCID]*ConnHandler
	writeToTunnelC  chan *GotaFrame
	readFromTunnelC chan *GotaFrame
	fastOpen bool
	quit    chan struct{}
	stopped bool
	mutex   sync.Locker
	mode    int
	useConnPool   bool
	connPool      *ConnPool
	connPoolCount int
	connPoolAlive int
}
func NewConnManager() *ConnManager {
	ncc := make(chan CCID)
	cleanCCID := make(chan CCID)
	cleanClientID := make(chan ClientID)
	chPool := make(map[CCID](*ConnHandler))
	wc := make(chan *GotaFrame)
	rc := make(chan *GotaFrame)
	q := make(chan struct{})
	l := &sync.Mutex{}
	ml := &sync.RWMutex{}
	d := make([]byte, 4)
	rand.Read(d)
	clientID := binary.LittleEndian.Uint32(d)
	return &ConnManager{
		clientID: clientID,
		newCCIDChannel:        ncc,
		cleanUpCHChanCCID:     cleanCCID,
		cleanUpCHChanClientID: cleanClientID,
		connHandlerPool:       chPool,
		writeToTunnelC:  wc,
		readFromTunnelC: rc,
		fastOpen: false,
		quit:     q,
		mutex:    l,
		poolLock: ml,
	}
}
func (cm *ConnManager) NewCCIDChannel() chan CCID {
	return cm.newCCIDChannel
}
func (cm *ConnManager) EnableFastOpen() {
	cm.fastOpen = true
}
func (cm *ConnManager) SetConnPool(n int, alive int) {
	cm.useConnPool = true
	if n == 0 {
		n = DefaultConnCount
	}
	cm.connPoolCount = n
	cm.connPoolAlive = alive
}
func (cm *ConnManager) WriteToTunnelChannel() chan *GotaFrame {
	return cm.writeToTunnelC
}
func (cm *ConnManager) ReadFromTunnelChannel() chan *GotaFrame {
	return cm.readFromTunnelC
}