package reuseport

import (
	

	ma 
	manet 
)

// Dial dials the given multiaddr, reusing ports we're currently listening on if
// possible.
//
// Dial attempts to be smart about choosing the source port. For example, If
// we're dialing a loopback address and we're listening on one or more loopback
// ports, Dial will randomly choose one of the loopback ports and addresses and
// reuse it.
func ( *Transport) ( ma.Multiaddr) (manet.Conn, error) {
	return .DialContext(context.Background(), )
}

// DialContext is like Dial but takes a context.
func ( *Transport) ( context.Context,  ma.Multiaddr) (manet.Conn, error) {
	, ,  := manet.DialArgs()
	if  != nil {
		return nil, 
	}
	var  *dialer
	switch  {
	case "tcp4":
		 = .v4.getDialer()
	case "tcp6":
		 = .v6.getDialer()
	default:
		return nil, ErrWrongProto
	}
	,  := .DialContext(, , )
	if  != nil {
		return nil, 
	}
	,  := manet.WrapNetConn()
	if  != nil {
		.Close()
		return nil, 
	}
	return , nil
}

func ( *network) ( string) *dialer {
	.mu.RLock()
	 := .dialer
	.mu.RUnlock()
	if  == nil {
		.mu.Lock()
		defer .mu.Unlock()

		if .dialer == nil {
			.dialer = newDialer(.listeners)
		}
		 = .dialer
	}
	return 
}