## [Android M] Disposing a large amount of sockets while a connect operation is in progress leads to an ART error on Android Marshmallow

_Submitted by a community member on 2016-03-24 14:45 UTC_

I made an app which needs to scan networks for specific hosts. More precisely I want to create a list of all hosts with whom the connection was successfully established on a specific port.
e.g. find all reachable hosts from 192.168.0.1 to 192.168.0.255 on port 80

The standard timeout for a connect operation is roughly 2 minutes and definitely too high for my purpose. Therefore I had to find a way to set a timeout. The only intended way to do this is by using the static CancelConnectAsync method of the Socket class. See <http://pastebin.com/1bBsj0MN>
Xamarin Android however does not support this method. My workaround for this is to forcefully stop the ongoing connect by closing the socket. So far it worked really well. On Android Marshmallow however the app crashes after doing so by no later than 255 times.

I attached a simple test case and created a video of it here:
<http://www.screencast.com/t/zIkKwvrinIx0>

The log can be found here:
<https://msdn.microsoft.com/library/system.net.sockets.socket.cancelconnectasync(v=vs.110).aspx>

This is probably the most important line.
03-24 10:17:54.031: A/art(1604): art/runtime/fault\_handler.cc:117] Check failed: !initialized\_

* * *

_Reference: <https://bugzilla.xamarin.com/show_bug.cgi?id=39888>_