// ZMQ_DELAY_ATTACH_ON_CONNECT: Accept messages only when connections are made
//
// See: http://api.zeromq.org/3-2:zmq-setsockopt#toc22
505056
func (soc *Socket) SetDelayAttachOnConnect(value bool) error {
	val := int(0)
	if value {
		val = 1
	}
	return soc.setInt(C.ZMQ_DELAY_ATTACH_ON_CONNECT, val)
}
212426
func (soc *Socket) SetLinger(value time.Duration) error {
	val := int(value / time.Millisecond)
	if value == -1 {
		val = -1
	}
	return soc.setInt(C.ZMQ_LINGER, val)
}
147134
func (md *MDServerRemote) OnConnect(ctx context.Context,
	conn *rpc.Connection, client rpc.GenericClient,
	server *rpc.Server) (err error) {

	defer func() {
		md.config.Reporter().OnlineStatusChanged(ctx, err == nil)
		if err == nil {
			md.config.Reporter().Notify(ctx,
				connectionNotification(connectionStatusConnected))
		}
	}()

	md.log.CInfof(ctx, "OnConnect called with a new connection")

	// we'll get replies asynchronously as to not block the connection
	// for doing other active work for the user. they will be sent to
	// the FolderNeedsRekey handler.
	if err := server.Register(keybase1.MetadataUpdateProtocol(md)); err != nil {
		if _, ok := err.(rpc.AlreadyRegisteredError); !ok {
			return err
		}
	}

	// reset auth -- using md.client here would cause problematic recursion.
	c := keybase1.MetadataClient{Cli: client}
	pingIntervalSeconds, err := md.resetAuth(ctx, c)
	switch err.(type) {
	case nil:
	case idutil.NoCurrentSessionError:
		md.log.CInfof(ctx, "Logged-out user")
	default:
		return err
	}

	md.config.KBFSOps().PushConnectionStatusChange(MDServiceName, nil)

	// start pinging
	md.pinger.resetTicker(pingIntervalSeconds)
	return nil
}
299401
func go_on_connect_callback(privateCtx unsafe.Pointer) C.int {
	log.Debug("go_on_connect_callback BEGIN")
	defer log.Debug("go_on_connect_callback END")
	context.lock.RLock()
	defer context.lock.RUnlock()

	// Get memif reference.
	ifIndex := int(uintptr(privateCtx))
	memif, exists := context.memifs[ifIndex]
	if !exists {
		return C.int(ErrNoConn.Code())
	}

	// Initialize Rx/Tx queues.
	err := memif.initQueues()
	if err != nil {
		if memifErr, ok := err.(*MemifError); ok {
			return C.int(memifErr.Code())
		}
		return C.int(ErrUnknown.Code())
	}

	// Call the user callback.
	if memif.callbacks.OnConnect != nil {
		memif.callbacks.OnConnect(memif)
	}

	// Start polling the RX queues for interrupts.
	for i := 0; i < len(memif.queueIntCh); i++ {
		memif.wg.Add(1)
		go pollRxQueue(memif, uint8(i))
	}

	return C.int(0)
}
164088
func (ln TCPKeepAliveListener) Accept() (c net.Conn, err error) {
	for {
		ln.SetDeadline(time.Now().Add(time.Second))
		select {
		case <-ln.cancel:
			return nil, errors.New("port closed")
		default:
		}

		conn, err := ln.AcceptTCP()
		if err != nil {
			switch err := err.(type) {
			case net.Error:
				if err.Timeout() {
					continue
				}
			}
			return nil, err
		}
		conn.SetKeepAlive(true)
		conn.SetKeepAlivePeriod(3 * time.Minute)

		// and return the connection.
		return conn, nil
	}
}
443954
func (soc *Socket) SetConnectRid(value string) error {
	if minor < 1 {
		return ErrorNotImplemented41
	}
	if value == "" {
		return soc.setNullString(C.ZMQ_CONNECT_RID)
	}
	return soc.setString(C.ZMQ_CONNECT_RID, value)
}
104173
func (s *Server) OnConnection(ws *websocket.Conn) {
	defer func() {
		ws.Close()
	}()

	client := NewClient(ws, s.hub)
	s.hub.Register(client)
	client.Listen()
}
737436
func (c *DefaultClient) Connect() error {
	if c.mqtt.IsConnected() {
		return nil
	}
	var err error
	for retries := 0; retries < ConnectRetries; retries++ {
		token := c.mqtt.Connect()
		finished := token.WaitTimeout(1 * time.Second)
		if !finished {
			c.ctx.Warn("mqtt: connection took longer than expected...")
			token.Wait()
		}
		err = token.Error()
		if err == nil {
			break
		}
		c.ctx.Warnf("mqtt: could not connect (%s), retrying...", err)
		<-time.After(ConnectRetryDelay)
	}
	if err != nil {
		return fmt.Errorf("Could not connect to MQTT Broker (%s)", err)
	}
	return nil
}
1112940
func (c *client) Bind() {
	delay := 1.0
	const maxdelay = 120.0
	for !c.closed() {
		eli := make(chan struct{})
		c.inbox = make(chan pdu.Body)
		conn, err := Dial(c.Addr, c.TLS)
		if err != nil {
			c.notify(&connStatus{
				s:   ConnectionFailed,
				err: err,
			})
			goto retry
		}
		c.conn.Set(conn)
		if err = c.BindFunc(c.conn); err != nil {
			c.notify(&connStatus{s: BindFailed, err: err})
			goto retry
		}
		go c.enquireLink(eli)
		c.notify(&connStatus{s: Connected})
		delay = 1
		for {
			p, err := c.conn.Read()
			if err != nil {
				c.notify(&connStatus{
					s:   Disconnected,
					err: err,
				})
				break
			}
			switch p.Header().ID {
			case pdu.EnquireLinkID:
				pResp := pdu.NewEnquireLinkRespSeq(p.Header().Seq)
				err := c.conn.Write(pResp)
				if err != nil {
					break
				}
			case pdu.EnquireLinkRespID:
				c.updateEliTime()
			default:
				c.inbox <- p
			}
		}
	retry:
		close(eli)
		c.conn.Close()
		close(c.inbox)
		delayDuration := c.BindInterval
		if delayDuration == 0 {
			delay = math.Min(delay*math.E, maxdelay)
			delayDuration = time.Duration(delay) * time.Second
		}
		c.trysleep(delayDuration)
	}
	close(c.Status)
}
942823
func ReconnectWait(t time.Duration) Option {
	return func(o *Options) error {
		o.ReconnectWait = t
		return nil
	}
}
395189
func (soc *Socket) GetConnectTimeout() (time.Duration, error) {
	if minor < 2 {
		return time.Duration(0), ErrorNotImplemented42
	}
	v, err := soc.getInt(C.ZMQ_CONNECT_TIMEOUT)
	return time.Duration(v) * time.Millisecond, err
}
911809
func (s *Socket) Connect(endpoint string) (err error) {
	cstr := C.CString(endpoint)
	defer C.free(unsafe.Pointer(cstr))
	r := C.zmq_connect(s.sock, cstr)
	if r == -1 {
		err = zmqerr()
	}
	return
}
1356499
func Heartbeat(delay time.Duration) ConnectionOption {
	// Proton-C divides the idle-timeout by 2 before sending, so compensate.
	return func(c *connection) { c.engine.Transport().SetIdleTimeout(2 * delay) }
}
744918
func (client *Client) Attach(cid string, options *AttachOptions) (io.ReadCloser, error) {
	values := url.Values{}
	add := func(name string, val bool) {
		values.Add(name, strconv.FormatBool(val))
	}
	add("logs", options.Logs)
	add("stream", options.Stream)
	add("stdin", options.Stdin)
	add("stdout", options.Stdout)
	add("stderr", options.Stderr)

	p := fmt.Sprintf("/containers/%s/attach?%s", cid, values.Encode())
	req, err := http.NewRequest("POST", client.URL.String()+p, nil)
	if err != nil {
		return nil, fmt.Errorf("could not construct request to docker")
	}
	resp, err := client.HTTPClient.Do(req)
	if err != nil {
		if !strings.Contains(err.Error(), "connection refused") && client.TLSConfig == nil {
			return nil, fmt.Errorf("%v. Are you trying to connect to a TLS-enabled daemon without TLS?", err)
		}
		return nil, err
	}
	if resp.StatusCode == http.StatusOK {
		return resp.Body, nil
	}

	resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusSwitchingProtocols:
		return nil, fmt.Errorf("docker: attach: did not send websocket request but got 101 back")
	case http.StatusBadRequest:
		// Probably shouldn't get here
		return nil, fmt.Errorf("docker: attach: invalid request parameters")
	case http.StatusInternalServerError:
		return nil, fmt.Errorf("docker: attach: internal server error")
	default:
		return nil, fmt.Errorf("docker: attach: unexpected status code %s", resp.Status)
	}
}
661581
func (k *Kite) OnConnect(handler func(*Client)) {
	k.handlersMu.Lock()
	k.onConnectHandlers = append(k.onConnectHandlers, handler)
	k.handlersMu.Unlock()
}
895193
func ZmqRequestSocket(ip string, request_ports []int) *zmq.Socket {
	context, _ := zmq.NewContext()
	socket, _ := context.NewSocket(zmq.REQ)
	for _, _port := range request_ports {
		socket.Connect(fmt.Sprintf("tcp://%s:%d", ip, _port))
	}
	return socket
}
300588
func ZmqRequest(_socket *zmq.Socket, dat ...string) (string, error) {
	_msg := strings.Join(dat, " ")
	_socket.Send([]byte(_msg), 0)
	response, err := _socket.Recv(0)
	return string(response), err
}