func (l *Listener) Dial(r io.Reader, w io.Writer) (*Conn, error) {
	l.init()
	c := &Conn{R: r, W: w}
	select {
	case <-l.closing:
		operr := &net.OpError{
			Op:   "dial",
			Net:  network,
			Addr: l.Addr(),
			Err:  neterr{timeout: false, err: listenerClosed},
		}
		return nil, operr
	case l.connc <- c:
		return c, nil
	}
}
Dial connects to a Listener. r and w may be nil; see Conn.
Note that r and w here are named from the server's perspective,
so data that you are sending across the connection will be read
from r, and responses from the connection will be written to w.
See the documentation in Conn.
package ionet
import (
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"sync"
	"time"
)
type Conn struct {
	R   io.Reader
	W   io.Writer
	rmu sync.Mutex  
	wmu sync.Mutex  
	closing   chan struct{}  
	closingmu sync.Mutex     
	rdead   time.Time     
	rdeadmu sync.RWMutex  
	wdead   time.Time     
	wdeadmu sync.RWMutex  
}
type nerr struct {
	n   int
	err error
}
func (c *Conn) Read(b []byte) (int, error) {
	if c.R == nil {
		return 0, c.readErr(false, io.EOF)
	}
	c.initClosing()
	select {
	case <-c.closing:
		return 0, c.readErr(false, connClosed)
	default:
	}
	c.rdeadmu.RLock()
	defer c.rdeadmu.RUnlock()
	if !c.rdead.IsZero() && c.rdead.Before(time.Now()) {
		return 0, c.readErr(true, timedOut)
	}
	readc := make(chan nerr, 1)
	go func() {
		c.rmu.Lock()
		n, err := c.R.Read(b)
		c.rmu.Unlock()
		readc <- nerr{n, err}
	}()
	var timeout <-chan time.Time
	timer := deadlineTimer(c.rdead)  
	if timer != nil {
		timeout = timer.C
		defer timer.Stop()
	}
	select {
	case <-c.closing:
		return 0, c.readErr(false, connClosed)
	case <-timeout:
		return 0, c.readErr(true, timedOut)
	case nerr := <-readc:
		if nerr.err != nil {
			return nerr.n, c.readErr(false, nerr.err)
		}
		return nerr.n, nil
	}
}
func (c *Conn) Write(b []byte) (int, error) {
	if c.W == nil {
		return ioutil.Discard.Write(b)
	}
	c.initClosing()
	select {
	case <-c.closing:
		return 0, c.writeErr(false, connClosed)
	default:
	}
	c.wdeadmu.RLock()
	defer c.wdeadmu.RUnlock()
	if !c.wdead.IsZero() && c.wdead.Before(time.Now()) {
		return 0, c.writeErr(true, timedOut)
	}
	writec := make(chan nerr, 1)
	go func() {
		c.wmu.Lock()
		n, err := c.W.Write(b)
		c.wmu.Unlock()
		writec <- nerr{n, err}
	}()
	var timeout <-chan time.Time
	c.wdeadmu.RLock()
	timer := deadlineTimer(c.wdead)  
	if timer != nil {
		timeout = timer.C
		defer timer.Stop()
	}
	select {
	case <-c.closing:
		return 0, c.writeErr(false, connClosed)
	case <-timeout:
		return 0, c.writeErr(true, timedOut)
	case nerr := <-writec:
		if nerr.err != nil {
			return nerr.n, c.writeErr(false, nerr.err)  
		}
		return nerr.n, nil
	}
}
func (c *Conn) Close() error {
	c.initClosing()
	c.closingmu.Lock()
	defer c.closingmu.Unlock()
	select {
	case <-c.closing:
		return &net.OpError{
			Op:   "close",
			Net:  network,
			Addr: c.LocalAddr(),
			Err:  neterr{timeout: false, err: connClosed},
		}
	default:
		close(c.closing)
	}
	return nil
}
func (c *Conn) Wait() {
	c.initClosing()
	<-c.closing
}
func (c *Conn) SetDeadline(t time.Time) error {
	c.SetReadDeadline(t)
	c.SetWriteDeadline(t)
	return nil
}
func (c *Conn) SetReadDeadline(t time.Time) error {
	c.rdeadmu.Lock()
	c.rdead = t
	c.rdeadmu.Unlock()
	return nil
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
	c.wdeadmu.Lock()
	c.wdead = t
	c.wdeadmu.Unlock()
	return nil
}
func (c *Conn) LocalAddr() net.Addr { return addr("local") }
func (c *Conn) RemoteAddr() net.Addr { return addr("remote") }
func deadlineTimer(t time.Time) *time.Timer {
	if t.IsZero() {
		return nil
	}
	return time.NewTimer(t.Sub(time.Now()))
}
func (c *Conn) initClosing() {
	c.closingmu.Lock()
	if c.closing == nil {
		c.closing = make(chan struct{})
	}
	c.closingmu.Unlock()
}
func (c *Conn) readErr(timeout bool, e error) *net.OpError {
	return &net.OpError{
		Op:   "read",
		Net:  network,
		Addr: c.RemoteAddr(),
		Err:  neterr{timeout: timeout, err: e},
	}
}
func (c *Conn) writeErr(timeout bool, e error) *net.OpError {
	return &net.OpError{
		Op:   "write",
		Net:  network,
		Addr: c.RemoteAddr(),
		Err:  neterr{timeout: timeout, err: e},
	}
}
type Listener struct {
	sync.Mutex                
	connc      chan *Conn     
	closing    chan struct{}  
}
func (l *Listener) init() {
	l.Lock()
	defer l.Unlock()
	if l.closing == nil {
		l.closing = make(chan struct{})
	}
	select {
	case <-l.closing:
	default:
		if l.connc == nil {
			l.connc = make(chan *Conn)
		}
	}
}
func (l *Listener) Accept() (net.Conn, error) {
	l.init()
	select {
	case conn := <-l.connc:
		return conn, nil
	case <-l.closing:
		operr := &net.OpError{
			Op:   "accept",
			Net:  network,
			Addr: l.Addr(),
			Err:  neterr{timeout: false, err: listenerClosed},
		}
		return nil, operr
	}
}
func (l *Listener) Close() error {
	l.init()
	l.Lock()
	defer l.Unlock()
	select {
	case <-l.closing:
		return &net.OpError{
			Op:   "close",
			Net:  network,
			Addr: l.Addr(),
			Err:  neterr{timeout: false, err: listenerClosed},
		}
	default:
		l.connc = nil
		close(l.closing)
	}
	return nil
}